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();
}
}
}
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With