如何从推送通知中获取json字符串值?

时间:2016-08-07 18:02:09

标签: ios json swift

我需要存储推送通知中的最后一个提醒json string值。

使用"aps: \(userInfo["aps"]!)"会返回整个json

aps: {
    alert = "last alert message";
}

但我只需要"last alert message"

这是我的代码:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.

    dUserInfo = userInfo
    print("aps: \(userInfo["aps"]!)")

    NSUserDefaults.standardUserDefaults().setObject(dUserInfo, forKey: "last_push")
    NSUserDefaults.standardUserDefaults().synchronize()     
}

如何正确收到消息?

1 个答案:

答案 0 :(得分:1)

试试这个:

if let aps = userInfo["aps"] as? [String: AnyObject] {
            if let alert = aps["alert"] as? String {
                NSUserDefaults.standardUserDefaults().setObject(alert, forKey: "last_push")
            }
        }