Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

iphone - For push notifications: how do I add action to alert view to change views?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
// 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"]){
    }
    ...
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...