【问题标题】:Swift Custom Data Push NotificationSwift 自定义数据推送通知
【发布时间】:2017-08-23 04:46:46
【问题描述】:

我正在尝试检索在推送通知中发送的自定义数据。

自定义数据为:

[{"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)")
        }

当我运行代码时它会在线崩溃:

让 customData = aps["u"] as!字符串

非常感谢任何帮助!

【问题讨论】:

    标签: swift push-notification


    【解决方案1】:

    如果我不正确但您选错了键,请首先原谅。

    if let aps = pushNotification["aps"] as? [String:Any] 
    

    应该是

    if let data = pushNotification["u"] as? [String:Any]
    

    然后你可以像这样得到测试的值

    data["test"]
    

    希望我是对的,这对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多