简体   繁体   English

当应用不在前台时接收服务中的 FCM 数据消息

[英]Receive FCM data messages in service when the app is not in Foreground

FCM documentation states that when app is in foreground, FCM messages are delivered to system tray. FCM 文档指出,当应用程序处于前台时,FCM 消息将传递到系统托盘。

I want to handle these messages in a FirebaseMessagingService.我想在 FirebaseMessagingService 中处理这些消息。 Is it possible to access the system tray to get the firebase messages (as I've already a scheduled Service) or to reactivate the FirebaseMessagingService.onMessageReceived behaviour as on older firebase API versions?是否可以访问系统托盘以获取 firebase 消息(因为我已经安排了服务)或重新激活 FirebaseMessagingService.onMessageReceived 行为,就像旧 firebase ZDB974238714CA8DE6FZA7 版本一样?

onMessageReceived is called only when the when there is no notification attached to the message.仅当消息没有附加通知时才调用onMessageReceived So for messages with both notification and payload, it won't be called.因此,对于同时具有通知和有效负载的消息,它不会被调用。

According to FCM documentation :根据FCM 文档

onMessageReceived is provided for most message types, with the following exceptions:大多数消息类型都提供了 onMessageReceived,但以下情况除外:

Notification messages delivered when your app is in the background .当您的应用程序在后台时传递的通知消息 In this case, the notification is delivered to the device's system tray.在这种情况下,通知将传送到设备的系统托盘。 A user tap on a notification opens the app launcher by default.默认情况下,用户点击通知会打开应用启动器。

Messages with both notification and data payload , when received in the background.在后台接收时带有通知和数据负载的消息 In this case, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.在这种情况下,通知被传递到设备的系统托盘,数据负载被传递到启动器 Activity 的附加内容中。

As you can see, onMessageReceived is called even if your app is in background.如您所见,即使您的应用程序在后台,也会调用onMessageReceived

The messages disappeared on a Xiaomi device due to power saving restriction.由于节电限制,消息在小米设备上消失了。 After changing batterie management settings data messages arrived更改电池管理设置后,数据消息到达

FCM message will show in system tray if your app is in background but if your app is in foreground your FirebaseMessagingService.onMessageReceived will trigger so you can show a dialog or snackBar.如果您的应用程序在后台,FCM 消息将显示在系统托盘中,但如果您的应用程序在前台,您的 FirebaseMessagingService.onMessageReceived 将触发,因此您可以显示对话框或snackBar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序处于前台状态时无法从 FCM 接收通知 - Cant receive Notifications from FCM while the app is in Foreground state 运行前台服务时如何检测当前的前台应用程序 - How to detect current foreground app when foreground service is running 使用 FCM 杀死应用程序时,Android 无法接收通知 - Android unable to receive notification when app is killed using FCM 当我的应用程序启动前台服务时,如果用户在设置中关闭了通知权限,那么服务是否在前台? - When my app starts a foreground service, is the service in foreground if user has turned off notification permission in settings? 当应用程序被杀死时,AlarmManager 在前台服务中不起作用 - AlarmManager does not work in foreground service, when app was killed 当应用程序在后台时从FCM消息中获取有效载荷数据 - Get payload data from FCM Message when app is in the background Android studio:当应用程序在前台时,无法在 sdk &lt; 26 上接收推送消息 - Android studio: cant recieve push messages on sdk < 26 when app is on foreground 隐藏应用程序后如何发送和接收数据? - How to send and receive a data when app is hidden? Smack Android,创建接收消息的服务 - Smack Android, creating a service to receive messages 应用程序终止后如何停止前台服务? - How to stop a foreground service after app kill?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM