我有通过Firebase发送的推送通知,当用户执行操作时,会有一个php脚本将推送发送到关联的FCM令牌。收到了推送通知,但是我试图从“ gcm.notification.payload”键中获取信息。我尝试将userInfo强制转换为Dictionary,NSDictionary,[String:Any],JSON,但没有任何效果。当我尝试说有效负载[“ type”]。string时,值为nil。这是我能够从有效负载字典中获取某些信息的最接近的地方,其中通知是收到的UNNotification:
let userInfo = notification.request.content.userInfo
let json = JSON(notification.request.content.userInfo)
guard let gcmNotificationPayload = json["gcm.notification.payload"].string else { return }
let payload = JSON(gcmNotificationPayload)
print("json \(json)")
print("payload \(payload)")
print("\(notification.request.content.userInfo)")
控制台输出:
json {
"gcm.message_id" : "0:1523811750249807%3990eb5d3990eb5d",
"aps" : {
"sound" : "default",
"badge" : 1,
"alert" : {
"body" : "Tap to Open Herds List",
"title" : "test4 test has added you to a herd!"
}
},
"gcm.notification.payload" : "{\"image\":\"https:\\\/\\\/www.test.com\\\/images\\\/users\\\/417\\\/5acd085c97c3a.jpg\",\"type\":\"6\",\"title\":\"test4 test has added you to a herd!\",\"message\":\"Tap to Open Herds List\"}"
}
payload {"image":"https:\/\/www.test.com\/images\/users\/417\/5acd085c97c3a.jpg","type":"6","title":"test4 test has added you to a herd!","message":"Tap to Open Herds List"}
[AnyHashable("gcm.message_id"): 0:1523811750249807%3990eb5d3990eb5d, AnyHashable("aps"): {
alert = {
body = "Tap to Open Herds List";
title = "test4 test has added you to a herd!";
};
badge = 1;
sound = default;
}, AnyHashable("gcm.notification.payload"): {"image":"https:\/\/www.test.com\/images\/users\/417\/5acd085c97c3a.jpg","type":"6","title":"test4 test has added you to a herd!","message":"Tap to Open Herds List"}]
将let有效负载行更改为此可以解决问题,并且此记录使用了SwiftyJSON:
let payload = JSON.init(parseJSON: gcmNotificationPayload)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句