GCM iOS可以';t转换接收到的消息对象
我正在使用用于iOS的GCM,收到我推送的消息,但消息看起来像以下对象:GCM iOS可以';t转换接收到的消息对象,ios,swift2,google-cloud-messaging,Ios,Swift2,Google Cloud Messaging,我正在使用用于iOS的GCM,收到我推送的消息,但消息看起来像以下对象: [aps: { alert = { body = "great match!"; title = "Portugal vs. Denmark"; }; }, gcm.message_id: 0:1464264430528872......] 以下是我收到消息时调用的整个函数: func application( application: UIApplication
[aps: {
alert = {
body = "great match!";
title = "Portugal vs. Denmark";
};
}, gcm.message_id: 0:1464264430528872......]
以下是我收到消息时调用的整个函数:
func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("Notification received: \(userInfo)")
print(userInfo)
// This works only if the app started the GCM service
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
handler(UIBackgroundFetchResult.NoData);
// [END_EXCLUDE]
}
我找不到如何获取警报正文和警报标题如何才能做到这一点?
userInfo
是[NSObject:AnyObject]类型的字典。要访问这些值,请使用订阅
“aps”键包含一个包含字典的字典,因此,例如,您可以执行以下操作:
if let aps = userInfo["aps"] as? [String:[String:String]],
alert = aps["alert"],
body = alert["body"],
title = alert["title"] {
print(title)
print(body)
}