我正在尝试检索推送通知中发送的自定义数据。
自定义数据是:
[{"test":"01"}]
pushNotification 的接收方式为:
[AnyHashable("aps"): { alert = "Hello World!"; sound = default;}, AnyHashable("u"): [{"test":"01"}] , AnyHashable("p"):2Q]
我接受的推送如下:
func onPushAccepted(_ pushManager: PushNotificationManager!, withNotification pushNotification: [AnyHashable : Any]!, onStart: Bool) {
print("Push notification accepted: \(pushNotification!)")
if let aps = pushNotification["aps"] as? [String:Any] {
let customData = aps["u"] as! String
print("json String:\(customData)")
}
当我运行代码时,它在线崩溃:
let customData = aps["u"] as! String
非常感谢任何帮助!
最佳答案
首先,如果我不正确,但您选择了错误的键,请原谅。
if let aps = pushNotification["aps"] as? [String:Any]
应该是
if let data = pushNotification["u"] as? [String:Any]
然后你可以得到这样的测试值
data["test"]
希望我是正确的,这会对您有所帮助
关于Swift 自定义数据推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43097882/