tardoandre tardoandre - 1 year ago 68
JSON Question

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

I need to store the last alert

value coming from a Push Notification.

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

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")

How can I get the message properly?

Answer Source

Try this:

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