Swift 读取远程通知的 userInfo [英] Swift read userInfo of remote notification

查看:23
本文介绍了Swift 读取远程通知的 userInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我收到这样的远程通知时,我实现了一个打开 AlertView 的功能:

I implemented a function to open an AlertView when I receive a remote notification like this:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
        var notifiAlert = UIAlertView()
        var NotificationMessage : AnyObject? =  userInfo["alert"]
        notifiAlert.title = "TITLE"
        notifiAlert.message = NotificationMessage as? String
        notifiAlert.addButtonWithTitle("OK")
        notifiAlert.show()
}

但 NotificationMessage 始终为零.

But NotificationMessage is always nil.

我的 json 负载如下所示:

My json payload looks like this:

{"aps":{"alert":"Testmessage","badge":"1"}}

我正在使用 Xcode 6、Swift 并且正在为 iOS8 进行开发.我现在搜索了几个小时,但没有找到任何有用的信息.通知工作完美......如果我点击它,警报视图会打开.我的问题是,我无法从 userInfo 中获取数据.

I am using Xcode 6, Swift and I am developing for iOS8. I searched hours now, but didn't find any useful information. The Notifications works perfectly.. and if I click it, the alertview opens. My problem is, that I am not able to get the data out of userInfo.

推荐答案

userInfo 字典的根级别项是 "aps",而不是 "alert".

The root level item of the userInfo dictionary is "aps", not "alert".

尝试以下操作:

if let aps = userInfo["aps"] as? NSDictionary {
    if let alert = aps["alert"] as? NSDictionary {
        if let message = alert["message"] as? NSString {
           //Do stuff
        }
    } else if let alert = aps["alert"] as? NSString {
        //Do stuff
    }
}

请参阅推送通知文档

这篇关于Swift 读取远程通知的 userInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆