识别推送通知消息 [英] Identifying the Push Notification Message

查看:25
本文介绍了识别推送通知消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想通过推送通知显示活动和优惠,但问题是,我能够显示活动或优惠,而不是同时显示两者.有什么方法可以识别推送通知的消息.代码如下:

In my project I want to show the events and offers through push notification, but the problem is, I'm able to show the events or offers, not both. Is there any way to identify the message of Push notification. Here's the code:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSString *message = nil;
    id alert = [userInfo objectForKey:@"alert"];

    if ([alert isKindOfClass:[NSString class]]) {
        message = alert;
    } else if ([alert isKindOfClass:[NSDictionary class]]) {
        message = [alert objectForKey:@"body"];
    }
    if (alert) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                                            message:@"AThe message." delegate:self
                                                  cancelButtonTitle:@"button 1"
                                                  otherButtonTitles:@"button", nil];
        [alertView show];
    }

    NSString *contentsInfo = [userInfo objectForKey:@"contTag"];
    NSLog(@"Received contents info : %@", contentsInfo);

    NSString *nibName = [AppDelegate fetchNibWithViewControllerName:@"EventsViewController"];

    EventsViewController *evc = [[EventsViewController alloc] initWithNibName:nibName bundle:nil];

    evc.newEvent = YES;

    [self.navigationController pushViewController:evc animated:YES];

    [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
}

推荐答案

alert 始终是一个 NSDictionary,有两个键:body 和 show-view.前者的值是警报消息,后者是Boolean(falsetrue).如果 false,则不显示警报的查看按钮.默认情况下显示查看"按钮,如果用户点击该按钮,则会启动应用程序.

alert is always an NSDictionary with two keys: body and show-view. The value of the former is the alert message and the latter is a Boolean (false or true). If false, the alert’s View button is not shown. The default is to show the View button which, if the user taps it, launches the application.

检查文档

要识别消息的类型,您可以提供其他字段,如这里

To identify type of the message you can provide additional fields, as described here

示例:

{
   "aps":{
      "badge":1,
      "alert":"This is my special message!",
      "mycustomvar1":"123456",
      "mycustomvar2":"some text",
      "myspecialtext":"This is the best!",
      "url":"http://www.mywebsite.com"
   }
}

这篇关于识别推送通知消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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