【问题标题】:Unable to parse gcm.notification.data from push notification无法从推送通知中解析 gcm.notification.data
【发布时间】:2019-05-12 22:01:00
【问题描述】:

我无法从 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;
    };
}]

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

【问题讨论】:

    标签: ios swift firebase-cloud-messaging unnotificationrequest


    【解决方案1】:

    你可以试试

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多