android - Firebase 通知 onMessageReceived 显示与应用程序在后台时相同的通知

标签 android firebase firebase-notifications

我正在从控制台向我的应用程序发送消息,并且我设法让应用程序在后台时按我想要的方式使用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/

相关文章:

android - 不要保留 Activity - 在真实场景中是否可能?

javascript - Firebase 函数 Express : How to get URL params with paths

push-notification - iOS 10 中如何接收后台通知?

android 上的 Android 推送通知声音丢失(不工作)背景和前景

android - 导航组件,检查返回堆栈中是否存在 fragment

java - 选择不包含主要类型。 eclipse 错误

android - 发送 USSD 代码安卓

ios - 有没有办法在主应用程序和扩展程序中对 Firebase 使用相同的匿名身份验证?

ios - 第一次后获取重复的表格 View 单元格

android - 当应用程序在前台并调用 onMessageReceived 时不显示通知