2017-04-18 3 views
1

swift3didReceiveRemoteNotificationからデータを取得する方法データを返しますか?

以下

でxcode8に実行すると、AppDelegate.swift

から自分のコード
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    print("==== didReceiveRemoteNotification ====") 
    print(userInfo) 
} 

私は通知を取得し、それをクリックすると、私のデバッグ領域がどのように

==== didReceiveRemoteNotification ==== 
[AnyHashable("type"): order, AnyHashable("aps"): { 
    alert = "[TestMessage]"; 
    badge = "<null>"; 
    category = alert; 
    sound = default; 
}, AnyHashable("url"): http://www.google.com] 

以下のようになるで私の質問は、 "www.google.com"のAnyHashable( "url")からデータを取得するためにuserInfoを使用するにはどうすればいいですか?

私は

print(userInfo[AnyHashable("url")]) 

を試みるが、デバッグ領域での出力が

Optional(http://www.google.com) 
+0

としてそれをキャストすることができ、更新の答えを確認してください –

答えて

2
if let url = userInfo["url"] as? String { 
    //do whatever you need with the url 
} 
2

ではprint(userInfo[AnyHashable("url")] as? String ?? "")を試してみてくださいまたはあなたがURL

2
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    if let aps = userInfo["aps"] as? NSDictionary { 
     print(aps) 
     let alert = (aps.object(forKey: "alert") as? String)! 
    } 

    let url = userInfo?["url"] as? String { 
     print(url) 
    } 
} 
関連する問題