根据我的经验,发生这种情况是因为您正在发送通知消息。正如谷歌解释的那样here https://firebase.google.com/docs/cloud-messaging/concept-options#notifications清楚地:
当应用程序处于后台时,通知消息将发送到通知托盘。对于前台应用程序,消息由回调函数处理。
现在,如果您想使用 Firebase Cloud Messaging 发送通知,并希望始终以您自己的自定义方式显示收到的通知,您可以使用FCM 数据消息不包含notification
部分,像这样:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"priority":"high",
"data":{
"title" : "Custom notif",
"body" : "This is a custom data notification!",
"action" : "A|B|C"
}
}
}
收到此通知后,Firebase 不会将其显示在托盘面板中,而是将其传递到您的应用程序(无论您的应用程序位于前台还是后台)。然后您可以使用中的字段data
部分用于创建和显示带有自定义操作的自定义通知。
您也可以将任何字段放入其中data
部分和字段,如title
只是例子。
客户端中的额外实现细节:
为了在 android(本机和/或反应本机)中处理数据消息,您可以使用远程消息(但使用不同的方式)。
反应本机:对于反应本机中仅处理数据通知,您可以使用此example https://rnfirebase.io/docs/v4.3.x/messaging/receiving-messages#4)-(Optional)(Android-only)-Listen-for-FCM-messages-in-the-background.
原生安卓:在原生android中你可以使用onMessageReceived(RemoteMessage remoteMessage)
您的服务的方法(实现 FirebaseMessagingService)。然后按照所解释的那样通过remoteMessage.getData()使用有效负载数据here https://firebase.google.com/docs/cloud-messaging/android/receive#override-onmessagereceived.