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

时间:2016-07-22 12:11:37

标签: android firebase firebase-notifications

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

Firebase console

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

enter image description here

我使用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());
}

2 个答案:

答案 0 :(得分:25)

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

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>");

答案 1 :(得分: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));
    }