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()
}

But NotificationMessage is always nil.

My json payload looks like this:

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

我正在使用Xcode6,SWIFT,我正在为iOS8开发. 我找了好几个小时了,但没有找到任何有用的信息. 通知工作正常.如果我单击它,alert 视图将打开. 我的问题是,我无法从userInfo中获取数据.

推荐答案

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

try 以下方法:

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
    }
}

See Push Notification Documentation

Json相关问答推荐

Jolt Change将对象添加到数组

如何在对象投影(*)上应用滤镜投影([?port==`eth1`])?

Bash和echo命令出现意外结果

如何在Gatsby/Reaction中获取JSON-File子 node

使用动态和可变JSON的图表多条

需要有关在Ffltter应用程序中解码JSON的帮助;未处理的异常:类型不是类型转换中类型的子类型

使用Kotlin限制序列化类属性的允许整数值

419(未知状态)使用laravel处理PUT请求

使用 jq 将非统一的 json 输出转换为汇总表

JQuery,使用 GET 方法发送 JSON 对象

反序列化大型 json 对象的 JsonMaxLength 异常

编写 JSON 日志(log)文件的格式?

在 Rails 中使用 JSON 创建嵌套对象

jquery用json数据填充下拉列表

在 Qt 4.7 中解析 JSON 的最简单方法

嵌套 JSON:如何向对象添加(推送)新项目?

在 HTML 数据属性上添加 JSON 是不是很糟糕?

MySQL Select JSON 字段属性具有值的位置

如何从 github API 解析链接头

杰克逊自动将 Joda DateTime 格式化为 ISO 8601 格式