fandro fandro - 1 year ago 142
iOS Question

GCM iOS can't convert message object received

I m using GCM for iOS and I receive the message that I push but the message look like this object :

[aps: {
alert = {
body = "great match!";
title = "Portugal vs. Denmark";
}, gcm.message_id: 0:1464264430528872......]

Here is the entire function called when I receive a message :

func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("Notification received: \(userInfo)")
// This works only if the app started the GCM service
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)

I don't find how to get the alert body and alert title how can I do that ?

Answer Source

userInfo is a dictionary of type [NSObject : AnyObject]. To access the values, use subscripting.

The "aps" key contains a dictionary which contains a dictionary, so, for example, you could do:

if let aps = userInfo["aps"] as? [String:[String:String]],
        alert = aps["alert"],
        body = alert["body"],
        title = alert["title"] {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download