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();
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…