且构网

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

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

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

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

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 通知有两种类型:通知消息和数据消息.

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 来发送数据消息.

阅读这篇了解更多详情.