How to get custom data from android firebase notification?

五迷三道 提交于 2020-02-01 04:01:10

问题


I'm trying to implement firebase notifications. But I have trouble finding any documentation on how to retrieve custom data from firebase notification.

But in in code how to get the custom key.

I'm using FirebaseMessagingService.onMessageReceived to get the message data.

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

回答1:


You can check your custom data using:

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

To get specific key:

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



回答2:


Let A be your key, then you can easily parse this value by using the following code.

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


来源:https://stackoverflow.com/questions/38526126/how-to-get-custom-data-from-android-firebase-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!