UIAlertView 上的 iOS 解析通知消息(空)

标签 ios objective-c json

我正在使用 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,您会看到它有两个键apsurlurl 的值为 http://www/google.comaps 的值又是一个字典。这本字典有一个键 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/

相关文章:

iphone - 无法从 AppDelegate 自定义我的选项卡栏

iphone - double 中的 objective-c stringvalue

ios - 制作完成 block 的问题 [iOS]

ios - Sprite Kit - 无法从对象获取自定义类数据

ios - Swift 获取 HTTP JSON 响应并使用 Codable 解析它

ios - 设置 AudioUnit 混响的参数?

ios - Swift 中的 CGContextDrawRadialGradient

ios - 查找 ipa 文件是 32 位还是 64 位。 (macos/unix)

java - 如何让 Glassfish 4.1.1 和 Jersey 使用 JSON?

json - 服务器发送的事件是否有可能以任何方式使浏览器过载?