对于推送通知:如何向警报视图添加操作以更改视图? [英] For push notifications: how do I add action to alert view to change views?

查看:84
本文介绍了对于推送通知:如何向警报视图添加操作以更改视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have push notifications sending to my application.

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

The code that triggers the alert is in my app delegate file (I think thats where it is supposed to go?)

如何对警报按钮进行操作,以便可以更改为其他视图?

How do I make an action for my alert button so that I can change to a different view?

推荐答案

// 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"]){
    }
    ...
    }

这篇关于对于推送通知:如何向警报视图添加操作以更改视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆