【问题标题】:For push notifications: how do I add action to alert view to change views?对于推送通知:如何向警报视图添加操作以更改视图?
【发布时间】:2011-06-25 00:36:49
【问题描述】:

所以我有推送通知发送到我的应用程序。

触发警报的代码在我的应用程序委托文件中(我认为那是它应该去的地方?)

如何为我的警报按钮执行操作,以便更改为不同的视图?

【问题讨论】:

  • 这是一个一般性的架构问题,还是您在寻找更具体的问题?如果你需要帮助,你应该发布一些代码。
  • 您需要告诉我们您的视图控制器层次结构。您当前的视图在哪里?

标签: iphone notifications push alert


【解决方案1】:
// Set Below code in your App Delegate Class... 

   - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
        // Call method to handle received notification
        [self apnsPayloadHandling:userInfo];
    }

    -(void) apnsPayloadHandling:(NSDictionary *)userInfo{
        // Example Payload Structure for reference
        /*
         remote notification: {
         acme1 = 11114;
         aps   =    {
         alert = {
         "action-loc-key" = "ACTION_BUTTON_TITLE";
         "loc-args" = ("MSG_TEXT");
         "loc-key" = "NOTIFICATION_DETAIL_PAGE";
         };
         badge = 10;
         sound = "chime";
         };
         }
         */


        //======================== Start : Fetching parameters from payload ====================

        NSString *action_loc_key;
        NSArray *loc_args_array;
        int badge;
        NSString *sound=@"";

        NSArray *payloadAllKeys = [NSArray arrayWithArray:[userInfo allKeys]];

        // getting "acme1" parameter value...
        if([payloadAllKeys containsObject:@"acme1"]){
            acme1 = [userInfo valueForKey:@"acme1"]; // getting "ID value" as "acme1"
        }

        NSString *localizedAlertMessage=@"";

        // getting "aps" parameter value...
        if([payloadAllKeys containsObject:@"aps"]){
            NSDictionary *apsDict = [NSDictionary dictionaryWithDictionary:[userInfo objectForKey:@"aps"]];

            NSArray *apsAllKeys = [NSArray arrayWithArray:[apsDict allKeys]];
            if([apsAllKeys containsObject:@"alert"]){

                if([[apsDict objectForKey:@"alert"] isKindOfClass:[NSDictionary class]]){

                    NSDictionary *alertDict = [NSDictionary dictionaryWithDictionary:[apsDict objectForKey:@"alert"]];

                    NSArray *alertAllKeys = [NSArray arrayWithArray:[alertDict allKeys]];

                    if([alertAllKeys containsObject:@"action-loc-key"]){
                        action_loc_key = [alertDict valueForKey:@"action-loc-key"]; // getting "action-loc-key"
                    }

                    if([alertAllKeys containsObject:@"loc-args"]){
                        loc_args_array = [NSArray arrayWithArray:[alertDict objectForKey:@"loc-args"]]; // getting "loc-args" array
                    }

                    if([alertAllKeys containsObject:@"loc-key"]){
                        loc_key = [alertDict valueForKey:@"loc-key"]; // getting "loc-key"
                    }


                    if([loc_args_array count] == 1){
                        localizedAlertMessage = [NSString stringWithFormat:NSLocalizedString(loc_key, nil),[loc_args_array objectAtIndex:0]];
                    }
                    else if([loc_args_array count] == 2){
                        localizedAlertMessage = [NSString stringWithFormat:NSLocalizedString(loc_key, nil),[loc_args_array objectAtIndex:0],[loc_args_array objectAtIndex:1]];
                    }
                    else if([loc_args_array count] == 3){
                        localizedAlertMessage = [NSString stringWithFormat:NSLocalizedString(loc_key, nil),[loc_args_array objectAtIndex:0],[loc_args_array objectAtIndex:1],[loc_args_array objectAtIndex:2]];
                    }
                }
                else{
                    localizedAlertMessage = [apsDict objectForKey:@"alert"];
                }
            }

            if([apsAllKeys containsObject:@"badge"]){
                badge = [[apsDict valueForKey:@"badge"] intValue]; // getting "badge" integer value
            }

            if([apsAllKeys containsObject:@"sound"]){
                sound = [apsDict valueForKey:@"sound"]; // getting "sound"
            }

        }

    //======================== Start : Handling notification =====================

    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
        if (state == UIApplicationStateActive){ // application is already open

         if(apnsAlert){
                apnsAlert = nil;
            }

            if(action_loc_key){ // View Button title dynamic...
                apnsAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ %@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"],NSLocalizedString(@"NOTIFICATION", nil)] message:localizedAlertMessage delegate:self cancelButtonTitle:NSLocalizedString(@"CANCEL", nil) otherButtonTitles: NSLocalizedString(action_loc_key, nil),nil];
            }
            else{
                apnsAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ %@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"],NSLocalizedString(@"NOTIFICATION", nil)] message:localizedAlertMessage delegate:self cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];
            }
            [apnsAlert show];
        }
    else{ // application is in background or not running mode
       [self apnsViewControllerRedirectingFor_loc_key:loc_key with_acme1:acme1];
    }

    }

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        // the user clicked one of the OK/Cancel buttons
        if(alertView == apnsAlert){
            if (buttonIndex == 1){
                [self apnsViewControllerRedirectingFor_loc_key:loc_key with_acme1:acme1];
            }
        }
    }

    -(void) apnsViewControllerRedirectingFor_loc_key:(NSString *)loc_key_para with_acme1:(NSString *)acme1_para{
    if([loc_key_para isEqualToString:@"NOTIFICATION_DETAIL_PAGE"]){

       DetailPageClass *detailPage = [[DetailPageClass alloc] initWithNibName:@"DetailPageClass" bundle:nil];
       [self.navigationcontroller pushViewController:detailPage animated:YES]; // use nav controller where you want to push...

    }
    else if([loc_key_para isEqualToString:@"NOTIFICATION_MAIN_PAGE"]){
    }
    ...
    }

【讨论】:

    【解决方案2】:

    要更改按钮的标题,请使用通知字典中的action-loc-key 键(请参阅this section of the guide)。

    要在点击通知时执行某些操作,您可以在应用委托中实现一些方法:Handling notifications

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多