【问题标题】:How to send JSON data via Parse Push Notification如何通过 Parse Push Notification 发送 JSON 数据
【发布时间】:2015-06-15 09:08:04
【问题描述】:

我想发送 JSON 数据到 Parse Push 通知我想发送这样的数据:

{ 
  "action": "android.intent.action.PUSH_STATE", 
  "alert": "welcome", 
   "message": "123123,123,123" 
}

我正在使用以下代码:

 NSDictionary * postDictionary = [NSDictionary dictionaryWithObjects:  [NSArray arrayWithObjects:@"action", @"alert",@"message" nil]
                                                                forKeys:[NSArray arrayWithObjects:@"android.intent.action.PUSH_STATE", @"welcome",@"123123,123,123", nil]];

    NSError * error = nil;
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONReadingMutableContainers error:&error];

    [push setMessage:jsonData];
    [push sendPushInBackground];

我收到以下错误:

无法使用 NSData* ___Strong 类型的 LValue 初始化 NSString* 类型的参数

【问题讨论】:

  • 我认为您必须先将 NSData 对象转换为 NSString 才能使用它。试试“[NSString stringWithUTF8String:[jsonData bytes]]”
  • 当我使用这个传递给解析的文本字符串时......我希望发送 json
  • 那么我认为你应该使用“setData”而不是“setMessage”。查看文档:parse.com/docs/ios/api/Classes/PFPush.html#//api/name/setData:
  • 看起来您在第一个 arrayWithObjects: 调用中、@"message" 之后和 nil 之前缺少一个逗号

标签: ios objective-c json xcode parse-platform


【解决方案1】:

JSON 有效负载记录在 REST API Push Notifications guide 中。 “alert”键应该是“data”散列中的一个字符串,而不是“aps”。此外,如果您还没有这样做,请确保在“oid”键中传递一个字符串。

一个有效的 JSON 负载应该是:

{
    "data": {
        "alert": "push_notification_msg",
        "sound": "",
        "cdata": {
            "type": 2,
            "oid": "XXX"
        },
        "action-loc-key": "push_notification_btn",
        "loc-key": "push_notification_msg"
    }
}

【讨论】:

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