是否有来自 Firebase Cloud Messaging 的回调以指示已收到消息?

is there a callback from Firebase Cloud Messaging to indicate that a message is received?

根据 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,
  // 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)

  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
}