对于推送通知:如何向警报视图添加操作以更改视图?

发布于 2024-11-17 06:02:18 字数 107 浏览 1 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

格子衫的從容 2024-11-24 06:02:18
// 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"]){
    }
    ...
    }
// 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"]){
    }
    ...
    }
宛菡 2024-11-24 06:02:18

要更改按钮的标题,请使用通知字典中的 action-loc-key 键(请参阅 本节指南)。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文