【问题标题】:iOS Parse Notification Message (null) on UIAlertViewUIAlertView 上的 iOS 解析通知消息(空)
【发布时间】:2015-12-29 19:06:45
【问题描述】:

我正在使用 Parse.com 将推送通知发送到一个简单的 iOS Web 视图应用程序,但我似乎无法让它显示我的 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

收到的消息:(空)

用户信息:{ ap = { alert = "推送标题到这里"; }; 网址 = "http://www.google.com"; }

我错过了什么吗?

【问题讨论】:

  • 请向我们展示NSLog(@"UserInfo %@" userinfo);的输出
  • @AnoopVaidya 我已经用 UserInfo 输出更新了我的帖子,谢谢。

标签: ios objective-c json


【解决方案1】:

如果您格式化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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多