我正在使用Parse.com向一个简单的iOS网页浏览应用发送推送通知,但我似乎无法通过我的JSON有效负载显示我的消息文本:
{
"alert": "Push Message goes here.",
"url": "http://www.google.com"
}
这是我的AppDelegate.m
:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSString* notifURL = [userInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifURL);
NSString* message = [userInfo objectForKey:@"alert"];
NSLog(@"Received Message: %@", message);
NSLog(@"UserInfo: %@", userInfo);
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alertPush = [[UIAlertView alloc]initWithTitle:@"My Webview App"
message:message
delegate:self
cancelButtonTitle:@"View"
otherButtonTitles:@"Cancel", nil];
[alertPush show];
[alertPush release];
objc_setAssociatedObject(alertPush, &aURL, notifURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
以下是我的日志返回的内容:
收到推送网址:http://www.google.com
收到消息:( null)
UserInfo:{ aps = { alert =“推送标题到此处”; }; url =“http://www.google.com”; }
我在这里错过了什么吗?
答案 0 :(得分:1)
如果您格式化UserInfo
,则会看到它有两个键aps
和url
。 url
的值为http://www/google.com
,aps
的值也是字典。此词典的键alert
的值为Push Title goes here
UserInfo: {
aps = {
alert = "Push Title goes here";
};
url = "http://www.google.com";
}
所以你需要通过以下方式提取它:
NSString *message = userInfo[@"aps"][@"alert"];
//First extract the dictionary with key : aps
//then extract the string with key : alert