我正在使用 Parse.com 将推送通知发送到一个简单的 iOS Web View 应用程序,但我似乎无法让它显示 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);
}
}
这是我的日志返回的内容:
Received Push Url: http://www.google.com
Received Message: (null)
UserInfo: { aps = { alert = "Push Title goes here"; }; url = "http://www.google.com"; }
我是不是漏掉了什么?
最佳答案
如果您格式化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
关于UIAlertView 上的 iOS 解析通知消息(空),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34517423/