Firebase Cloud Messaging是否有回调指示收到消息?

问题描述

here中的文档中

,有两种接收推送通知方法。但正如评论所言,只有当用户后台运行应用程序时,才点击通知

func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  // If you are receiving a notification message while your app is in the background,// this callback will not be fired till the user taps on the notification launching the application.
  // Todo: Handle data of notification

  // With swizzling disabled you must let Messaging kNow about the message,for Analytics
  // Messaging.messaging().appDidReceiveMessage(userInfo)

  // Print message ID.
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }

  // Print full message.
  print(userInfo)
}

func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  // If you are receiving a notification message while your app is in the background,for Analytics
  // Messaging.messaging().appDidReceiveMessage(userInfo)

  // Print message ID.
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }

  // Print full message.
  print(userInfo)

  completionHandler(UIBackgroundFetchResult.newData)
}

我想要的是,每当有推送通知到来时,无论用户后台还是在前台点击推送通知,我都想采取一些措施。所以每当收到消息时我都需要回调

在Android中,我可以在onMessageReceived回调中完成

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        
    }

是否有类似iOS的等效回调?我找不到

解决方法

您可以通过在通知有效负载中指定content_available来向iOS应用发送静默通知。设置为true时,通知将在后台唤醒您的应用,并且您可以使用application(_:didReceiveRemoteNotification:fetchCompletionHandler:)委托方法处理该通知。

您可以找到有关here的更多信息。

"to" : "[token]","content_available": true,"priority": "high","data" : {
  "key1" : "abc","key2" : abc
}