1

앱이 있습니다. FCM을 사용하여 알림을 푸시합니다. 메시지의 json은 다음과 같이 보입니다.iOS Firebase 클라우드 메시징 앱 닫기시 데이터 가져 오기

{ "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에서 다음 코드 스 니펫을 응용 프로그램 대리인에 갖게됩니다.

두 개의 userNotificationCenter 방법이 있습니다. 첫 번째 앱이 포 그라운드 일 때 호출됩니다. 트레이에서 밀어 넣기 알림을 누르면 두 번째 호출됩니다.

전체 코드는 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() 
    } 
} 
관련 문제