如何从 android firebase 通知中获取自定义数据?

How to get custom data from android firebase notification?

我正在尝试实施 Firebase 通知。但是我找不到任何关于如何从 firebase 通知中检索自定义数据的文档。

但在代码中如何获取自定义密钥。

我正在使用 FirebaseMessagingService.onMessageReceived 获取消息数据。

@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());
}

让 A 成为您的键,然后您可以使用以下代码轻松解析此值。

JSONObject json = new JSONObject(remoteMessage.getData());
    Iterator itr = json.keys();
    while (itr.hasNext()) {
    String key = (String) itr.next();
           if (key.equals("A")) {
                flag = json.getString(key);
           }
           Log.d(TAG, "..." + key + " => " + json.getString(key));
    }

您可以使用以下方式检查您的自定义数据:

for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    Log.d(TAG, "key, " + key + " value " + value);
}

获取特定密钥:

String value = remoteMessage.getData().get("<YOUR_KEY>");