网络服务通过FCM在Android上向我发送以下通知:
{
"registration_ids": [
"123456"
],
"priority": "high",
"data": {
"notification_type": "request_accepted",
"feedback": "request accepted"
}
}
然而,在Android上,在onMessageReceived
中,它变为:
{
google.sent_time=1525800030866,
notification_type=request_accepted,
google.ttl=2419200,
feedback=request accepted,
from=875234321904,
google.message_id=0:1525800030873951%137ecd
}
以上显然是无效的JSON。为什么会这样,如何解决?
有效负载中还有其他数据可以通过正确的JSON格式(:而不是=)来实现。但是这些参数的格式不正确。
编辑: onMessageReceived
:
public void onMessageReceived(RemoteMessage remoteMessage)
{
try
{
if(remoteMessage.getData() != null)
{
parseJson(remoteMessage.getData().toString());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private Object parseJson(String json)
{
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<ClientRequestAcceptedModel> responseAdapter = moshi.adapter(ClientRequestAcceptedModel.class);
try
{
return responseAdapter.fromJson(json);
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
更多JSON对象
{
"notification_type": "request_accepted",
"partner_information": "{\"zip\":\"24000\"}",
"feedback": "request accepted",
"request_information": "{\"request_type\":\"custom\",\"status\":0}"
}
合作伙伴信息和请求信息不会按照应有的方式显示。
答案 0 :(得分:0)
这是预期的行为
当你调用remoteMessage.getData()时,你将获得一张地图。
Map<String, String> params = remoteMessage.getData();
要获取json,您需要添加以下内容
JSONObject jobject = new JSONObject(params);
Log.d("This is the json", jobject());
所以你的代码应该是
Map<String, String> params = remoteMessage.getData();
if(params != null)
{
JSONObject jobject = new JSONObject(params);
parseJson(jobject.toString());
}