For push notifications: how do I add action to ale

2019-01-27 04:37发布

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?

2条回答
Anthone
2楼-- · 2019-01-27 05:16

To change the title of the button, use the action-loc-key key in the notification dictionary (see this section of the guide).

To do something when the notification is tapped, you can implement a few methods in your app delegate: Handling notifications.

查看更多
干净又极端
3楼-- · 2019-01-27 05:20
// 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"]){
    }
    ...
    }
查看更多
登录 后发表回答