xcode 识别推送通知消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12294214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identifying the Push Notification Message
提问by Joker
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];
}
回答by Alexey Petushkov
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.
alert
总是一个NSDictionary
有两个键:body 和 show-view。前者的值是警报消息,后者的值是Boolean
(false
或true
)。如果false
,则不显示警报的查看按钮。默认是显示查看按钮,如果用户点击它,就会启动应用程序。
check the docs
检查文档
To identify type of the message you can provide additional fields, as described here
要确定您可以提供附加字段消息的类型,描述在这里
Example:
例子:
{
"aps":{
"badge":1,
"alert":"This is my special message!",
"mycustomvar1":"123456",
"mycustomvar2":"some text",
"myspecialtext":"This is the best!",
"url":"http://www.mywebsite.com"
}
}