用户单击时如何获取 Firebase 通知正文?

How can I get Firebase Notification Body when user clicks?

当用户点击 Android 中的 Firebase 通知时,您可以获得以下键

使用以下代码:

if (getIntent().getExtras() != null) {
            for (String key : getIntent().getExtras().keySet()) {
                String value = getIntent().getExtras().getString(key);
                Log.d(TAG, "Key: " + key + " Value: " + value);
            }
        }

但是,我需要从消息中获取 Body。可能吗?

您必须手动处理通知,只需按照this guide

要获取通知正文,请在 onMessageReceived 方法中调用 remoteMessage.getNotification().getBody()

然后在显示通知时(查看 this file),将正文作为额外内容添加到意图中。

intent.putExtra("body", notificationBody);
intent.putExtra("title", notificationTitle);

希望这对您有所帮助:)

如果应用程序在后台,则不是,通知负载中定义的值实际上是为了生成通知。如果您想要访问在消息的通知负载中定义的值,那么您应该在数据负载中重复它们,这样当用户点击通知时,您将能够从意图的额外内容中检索它们。类似于:

{
  "to": "/topics/sometopic",
  "notification": {
    "title": "some title",
    "body": "some body"
  },
  "data": {
    "noti_title": "some title",
    "noti_body": "some body",
    "other_key": "other value"
  }
}