2016-05-26 5 views
1

を受けて変換することはできません。このオブジェクトのようになります。ここではGCMのiOSは、メッセージオブジェクトは、私はiOS用GCMを使用してMと私は私がプッシュしたメッセージが、メッセージを受信

[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] 
    } 

アラートの本文とアラートのタイトルを取得する方法はありますか?

答えて

1

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) 
} 
関連する問題