Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM Data payload received in android not in json format

I am getting the data payload from the firebase not in json format, instead I am getting custom key-value pairs as following format:

Data Payload:{image=https://www.xxxx.xxx/get-profile-picture, message=This is a test message., senderName=Mathew John}

I have to parse the data using Json parsing for further processing. Here is my code:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            String title = remoteMessage.getData().get("senderName");
            System.out.println("raja" + title);
            String msg = remoteMessage.getData().get("message");
            System.out.println("raja" + msg);
            sendMessage(msg,title);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
like image 481
syam Avatar asked Apr 11 '18 04:04

syam


1 Answers

I am getting the data payload from the firebase not in json format

Yes, Its behaving as intended.

Because Data payload contains custom key-value pairs not a JSON format

I have to parse the data using Json parsing for further processing.

You need to use Map<String, String> to convert data payload in to a JSONObject

check below example

SAMPLE CODE

Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
Log.e("JSON_OBJECT", object.toString());
like image 192
AskNilesh Avatar answered Oct 24 '22 22:10

AskNilesh