如何在didReceiveRemoteNotification中获取userInfo JSON值
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { PFPush.handlePush(userInfo) if application.applicationState == UIApplicationState.Active { PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo) if let msg = userInfo["test"] as? Dictionary { print(msg) } }
我的userInfo [“test”] JSON值是:
> Printing description of userInfo: ▿ 3 elements ▿ [0] : 2 elements - .0 : message - .1 : 1235 ▿ [1] : 2 elements - .0 : aps - .1 : 0 elements ▿ [2] : 2 elements - .0 : link - .1 :
我可以获取我的userInfo数据,但我不知道为什么我的msg没有值(甚至没有任何东西可以显示)我怎么去打印(msg)?
更新
这是我的推动:
$ this-> parsepush-> send(array(“channels”=> [‘test’],“data”= >> $ post_data));
在$ post_data示例中:
$ post_data = json_encode(array(’link’=> $ link,’message’=> $ message)); \
我试过了指令,但我也做不了简单的打印。
从userInfo获取值
编辑
仍然跳过“aps”部分。
图片1
我的JSON
这是我的Json
看起来像:
> { "aps":{ "alert":{ "title":"$title", "body":"$message" }, "badge":"1" }, "adira":{ "link":"$link" } }
这是我的代码:
> func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { // PFPush.handlePush(userInfo) // PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo) if let msg = userInfo["message"] as? String { print(msg) print("a") } if let alertDict = userInfo["aps"]?["alert"] as? Dictionary { print("title : ", alertDict["title"]) print("body :", alertDict["body"]!) print("badge: ", alertDict["badge"]!) print("b") } if let link = userInfo["link"] as? String { print(link) print("c") } } }
但我仍然无法得到我的aps
数据。 仍然没有或没有。
如果您需要特定代码,只需点击以下链接:
完整代码AppDelegate.swift
编辑2
我直接使用print(userInfo),打印输出为:
对象已保存。 [消息:iOS推送,aps:{},链接:]
更新2
我试图从parse.com推送,而不是我的网站(第三方)。 这是我从普通打印(userInfo)得到的
> [aps: { alert = 12345abc; sound = default; }]
所以我想我会改变并在我的JSON中添加一些内容从我的网站到:
[aps:{ alert = $message; sound = default; link = $link; }]
那样的东西?
正如评论中所提到的,APNS有效载荷中没有关键test
。
如果保证始终发送密钥message
的值,您可以轻松地打开该值
let msg = userInfo["message"] as! String print(msg)
否则使用可选绑定
if let msg = userInfo["message"] as? String { print(msg) }
从aps
键获取alert
字典并打印例如body
字符串使用
if let alertDict = userInfo["aps"]?["alert"] as? Dictionary { print("body :", alertDict["body"]!) }