1

私はアプリを持っています。 FCMを使用して通知をプッシュします。メッセージのjsonは、次のようになります。iOS Firebase Cloud Messagingアプリを閉じるとデータを取得する

{ "to": "xxx", "notification" : { 
      "body" : "Hi", 
      "badge" : 1, 
      "sound" : "default" 
     }, 
     "data" :  { 
      "id" : "xxx", 
      "first_name" : "xxx", 
      "last_name" : "xxx", 
      "full_name" : "xxx", 
      "primary_image" : "xxx", 
      "matchid" : "xxx", 
      "type": "match"/"message" 
     }, 
     "content_available": true, 
     "priority": "high" 
} 

私の通知に触れるときに起動する画面を検出するためのデータには「タイプ」があります。 == "match" - > MatchVCに行き、タイプ== "message" - > MessageVCに移動します。私のアプリがフォアグラウンドにある場合、私はdidReceiveRemoteNotification:userinfoからデータに到達することができますが、プッシュスクリーンを検出することができますが、私のアプリがバックグラウンドまたはクローズであれば、データなしの通知のみをdidReceiveRemoteNotification:userinfoから得ることができます。通知をクリックすると、アプリが開きます。すべてのソリューションが評価されます。

答えて

0

Firebase iOS SDKでは、アプリケーションデリゲートに次のコードスニペットがあります。

注意2つのuserNotificationCenterメソッドがあります。最初のものは、アプリケーションがフォアグラウンドのときに呼び出されます。トレイからプッシュ通知をタップすると、2が呼び出されます。

Firebase Official Githubリポジトリ(iOSクイックスタート)から完全なコードを見つけることができます。 https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift

@available(iOS 10, *) 
extension AppDelegate : UNUserNotificationCenterDelegate { 

    // Receive displayed notifications for iOS 10 devices. 
    func userNotificationCenter(_ center: UNUserNotificationCenter, 
           willPresent notification: UNNotification, 
           withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     let userInfo = notification.request.content.userInfo 
     // Print message ID. 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     // Print full message. 
     print("userInfo 1st") 
     print(userInfo) 

     let id = userInfo["id"] 
     let firstName = userInfo["first_name"] 

     print(id ?? "") 
     print(firstName ?? "") 

     // Change this to your preferred presentation option 
     completionHandler([]) 
    } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, 
           didReceive response: UNNotificationResponse, 
           withCompletionHandler completionHandler: @escaping() -> Void) { 
     let userInfo = response.notification.request.content.userInfo 
     // Print message ID. 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     // Print full message. 
     print("userInfo 2nd") 
     print(userInfo) 

     let id = userInfo["id"] 
     let firstName = userInfo["first_name"] 

     print(id ?? "") 
     print(firstName ?? "") 

     completionHandler() 
    } 
} 
関連する問題