Firebase 通知 onMessageReceived 显示与应用程序在后台时相同的通知
Firebase notification onMessageReceived show the same notification as when app in background
我正在从控制台向我的应用程序发送消息,并且我设法让该应用程序在后台时按我的意愿做出反应。所以基本上 SDK 会显示一个通知,当用户点击它并且应用程序启动时,我有一些自定义数据字段可用于构建对话消息。
因为我也希望应用程序在前台时做出反应,所以我添加了该服务,并且我注意到 RemoteMessage
还包含一个 getNotification()
方法,该方法 returns 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());
}
请参阅 "Override onMessageReceived" 下 receiving downstream messages on Android 的 Firebase 云消息传递文档。
我正在从控制台向我的应用程序发送消息,并且我设法让该应用程序在后台时按我的意愿做出反应。所以基本上 SDK 会显示一个通知,当用户点击它并且应用程序启动时,我有一些自定义数据字段可用于构建对话消息。
因为我也希望应用程序在前台时做出反应,所以我添加了该服务,并且我注意到 RemoteMessage
还包含一个 getNotification()
方法,该方法 returns 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());
}
请参阅 "Override onMessageReceived" 下 receiving downstream messages on Android 的 Firebase 云消息传递文档。