7

我正在尝试实施 Firebase 通知。但是我很难找到有关如何从 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());
}
4

2 回答 2

30

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

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>");
于 2016-07-22T12:53:44.697 回答
6

让 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));
    }
于 2016-07-22T12:25:58.597 回答