gpt4 book ai didi

json - 从 Apple 推送通知解析数据

转载 作者:可可西里 更新时间:2023-11-01 01:09:02 27 4
gpt4 key购买 nike

我有通过 Firebase 发送的推送通知,当用户执行操作时,有一个 php 脚本将推送发送到关联的 FCM token 。正在接收推送通知,但我正在尝试从“gcm.notification.payload”键获取信息。我尝试将 userInfo 转换为字典、NSDictionary、[String:Any]、JSON,但没有任何效果。当我尝试说 payload["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 payload 行更改为此解决了问题,这是使用 SwiftyJSON 进行记录:

let payload = JSON.init(parseJSON: gcmNotificationPayload)

关于json - 从 Apple 推送通知解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49844686/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com