tardoandre tardoandre - 4 months ago 13
JSON Question

How can I get the json string value from a Push Notification?

I need to store the last alert

json
string
value coming from a Push Notification.

Using
"aps: \(userInfo["aps"]!)"
returns the whole
json
:

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


but I only need
"last alert message"
.

This is my code:

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


How can I get the message properly?

Answer

Try this:

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