ios - Swift didReceiveRemoteNotification userInfo 无法获取值

标签 ios swift push-notification

我使用推送通知,并具有以下功能:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if let msg = userInfo["msg"] as? NSObject {
        println(msg)
    }
    GCMService.sharedInstance().appDidReceiveMessage(userInfo);
    NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
        userInfo: userInfo)
}

这打印了我:

{"model":"que","data":{"id":101,"vehicle":{"license_plate":"test","taxi":{"id":1,"logo":"/media/logos/mega_CxRn739.png.75x75_q85_crop.png","name":"Mega","city":"Novi Pazar","country":"Srbija"},"label":"A01"}}}

// prettify:
{
    "model": "que",
    "data": {
        "id": 101,
        "vehicle": {
            "license_plate": "test",
            "taxi": {
                "id": 1,
                "logo": "/media/logos/mega_CxRn739.png.75x75_q85_crop.png",
                "name": "Mega",
                "city": "Novi Pazar",
                "country": "Srbija"
            },
            "label": "A01"
        }
    }
}

现在当我使用时:

if let msg = userInfo["msg"] as? NSObject {
    println(msg["model"])
    println(msg["data"])
}

我无法构建:

'NSObject' does not have a member named 'subscript'

我怎样才能让它发挥作用?我需要在此之后获得所有属性,但无法执行第一步。

图片: enter image description here

修复:

if let msg = userInfo["msg"] as? String {
    if let data = msg.dataUsingEncoding(NSUTF8StringEncoding) {
        if let jsonObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: nil) {
            var data = JSON(jsonObject!)
            println(data["data"])
        }
    }
}

如果有人对此修复有建议,请告诉我..谢谢。

最佳答案

if let msg = userInfo["msg"] as? String {
    if let data = msg.dataUsingEncoding(NSUTF8StringEncoding) {
        if let jsonObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: nil) {
            var data = JSON(jsonObject!)
            println(data["data"])
        }
    }
}

如果有人对此修复有建议,请告诉我..谢谢。

关于ios - Swift didReceiveRemoteNotification userInfo 无法获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31223835/

相关文章:

android - 循环遍历android sqlite数据库

mobile - 如何使用RabbitMQ发送移动推送通知?

ios - 想使用 UIPasteBoard 将 gif 粘贴到邮件窗口

ios - 无法将表达式的类型 'NSDictionary' 转换为类型 'StringLiteralConvertible'

ios - 我可以使用 NSTimer 的 scheduledTimerWithTimeInterval 方法 1 天(24 小时)有或没有重复吗?

swift - 过滤Firebase数据[iOS]

ios - 为 React Native 设置 Realm 时遇到问题 : `rnpm link realm` stalls without further info or output

Swift 函数不在更新 View 中绘制圆圈

ios - 手动安装 Crypto swift 库。意外的平台状况

android - 如何在需要时在服务器上发送设备的位置