我正在从控制台向我的应用程序发送消息,并且我设法让应用程序在后台时按我想要的方式使用react。因此,基本上 SDK 会显示一条通知,当用户点击它并且应用程序启动时,我有一些自定义数据字段可用于构建对话框消息。
因为我也希望应用程序在前台使用react,所以我添加了该服务,并且我注意到 RemoteMessage
还包含一个 getNotification()
方法,该方法返回 RemoteMessage.Notification
我认为 SDK 使用它来在应用程序处于后台时显示通知。
有没有一种简单的方法可以简单地使用检索到的通知并显示它,就像应用程序处于后台时一样?
最佳答案
确实有。只需覆盖 FirebaseMessagingService.onMessageReceived
并获取通知正文,如 Firebase Cloud Messaging 文档中的此示例所示:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
请参阅 receiving downstream messages on Android 的 Firebase 云消息传递文档在“覆盖 onMessageReceived”下。
关于android - Firebase 通知 onMessageReceived 显示与应用程序在后台时相同的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38329477/