gpt4 book ai didi

ios - 无法从推送通知解析 gcm.notification.data

转载 作者:行者123 更新时间:2023-11-28 05:48:44 25 4
gpt4 key购买 nike

我无法从 Firebase 推送通知中获取 Data 字典。

我的代码:

class func parseIncommingMessages(notification:UNNotification) {
print("PushNotifications: \n \(notification.request.content.userInfo.debugDescription)")

let userInfo = notification.request.content.userInfo as! [String: Any]
let dicDataContainer = userInfo["gcm.notification.data"] as? [AnyHashable:Any]

// notification
if let dicDataMain = dicDataContainer?["Data"] as? [String:Any] , let notificationType = dicDataContainer?["notificationType"] as? String
{
print("\nData: \(dicDataMain)")
print("\n NotificationType: \(notificationType)")
}

}

推送通知:

[AnyHashable("gcm.message_id"): 0:1544560847941727%a27b0018a27b0018, AnyHashable("google.c.a.e"): 1, AnyHashable("gcm.notification.data"): {"Data":{"Name":"a","message":"test","operation":"change"},"notificationType":"status_change"}, AnyHashable("aps"): {
alert = {
body = Somthing;
title = Somthing;
};
}]

我无法解析,请帮助我......

最佳答案

你可以试试

let str = userInfo["gcm.notification.data"] as! String
let res = try? JSONDecoder().decode(Root.self,data:str.data(using:.utf8)!)
print(res?.data)

struct Root : Decodable {
let data: DataClass
let notificationType: String
}

struct DataClass : Decodable {
let name, message, operation: String
}

关于ios - 无法从推送通知解析 gcm.notification.data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53732167/

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