Swift 自定义数据推送通知

标签 swift push-notification

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

自定义数据是:

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

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

let customData = aps["u"] as! String

非常感谢任何帮助!

最佳答案

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

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

应该是

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

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

data["test"]

希望我是正确的,这会对您有所帮助

关于Swift 自定义数据推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43097882/

相关文章:

swift - 声明字典的字典没有值(value)

ios - 日期选择器文本字段中的核心数据日期比较

swift 。在子类中使用扩展属性类型

php - 方形通知 android(带 GCM)

python - 如何使用 python 连接 gcm 服务器?

swift - 使用 reduce 对自定义对象数组内的属性值求和

android - GCM 有多少个 3rd 方服务器可以使用单个服务器 key ?

push-notification - APNS 或 GCM 设备 token 验证

android - 系统刚启动时未收到 FCM 通知

ios - 添加可访问性自定义 slider iOS