且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android应用中未收到Firebase云推送通知

更新时间:2021-06-30 21:46:07

我遇到了类似的问题.我已经尝试了一切-我认为主要原因是所有服务迟早都会被系统杀死.唯一的方法是确保将通知传递到系统托盘而不是应用程序.为此,您需要使用数据消息通知.

I've had a similar issue. I've tried everything - in my opinion the main reason is that all the services are, sooner or later, being killed by the system. The only way is to make sure to deliver the notification to the system tray not to the application. To do that you need to use Data message notifications.

FCM通知有2种类型:通知消息和数据消息.

There are 2 types of FCM notifications: Notification message and Data message.

数据消息传递到系统托盘,并且始终显示-即使服务未运行.

Data messages are delivered to system tray and are always display - even if service is not running.

通知消息如下:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}

并触发FirebaseMessagingService的方法OnMessageReceaved().许多设备(尤其是华为和小米)都竭尽全力杀死后台服务,以防止电池耗尽.因此,FirebaseMessagingService并不是处理通知的***方法.

and triggers method OnMessageReceaved() of FirebaseMessagingService. Many devices (especially Huawei and Xiaomi) try to do everything to kill background services to prevent battery drain. So the FirebaseMessagingService isn't the best way to handle notifications.

第二种类型是数据消息:

Second type is Data message:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data":{
      "Nick" : "Mario",
      "body" : "great match!",
      "Room" : "PortugalVSDenmark"
    }
  }
}

此类型由系统托盘处理,因此您无需运行任何服务即可获取通知.它的方法要方便得多,但是据我所知,它不能通过控制台来实现.

This type is handled by the system tray, so you don't need any of service running to get the notification. Its much more convenient method, but as far i know, it can't be achieved with the console.

您可能需要服务器API才能发送数据消息.

阅读以了解更多详细信息.

Read this for more details.