相关疑难解决方法(0)

如何解析Android中的Json数据for Firebase Cloud Messaging(FCM)

FCM用于推送消息和处理onMessageReceived中的所有传入推送通知.现在的问题是解析嵌入在这个函数中的嵌入式jsonremoteMessage.getData()

我有以下块作为设备中的推送通知.数据有效载荷的内容可以在这里变化,这是经销商以后可以productInfo

{
  "to": "/topics/DATA",
  "priority": "high",
  "data": {
    "type": 6,
    "dealerInfo": {
      "dealerId": "358",
      "operationCode": 2
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我如何解析它

 if(remoteMessage.getData()!=null){

        JSONObject object = null;
        try {
            object = new JSONObject(remoteMessage.getData());       

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
Run Code Online (Sandbox Code Playgroud)

现在我获取带有黑色斜杠的数据作为remoteMessage.getData()返回,Map<String,String>所以可能我的嵌套块正在转换为字符串,但不确定.

{
  "wasTapped": false,
  "dealerInfo": "{\"dealerId\":\"358\",\"operationCode\":2}",
  "type": "6"
}
Run Code Online (Sandbox Code Playgroud)

如果我写,object = new JSONObject(remoteMessage.getData().toString());那么它会因以下通知而失败

{
  "to": "regid",
  "priority": "high",
  "notification" : {
      "body": "Message Body",
      "title" : "Call Status",
      "click_action":"FCM_PLUGIN_ACTIVITY"
   }, …
Run Code Online (Sandbox Code Playgroud)

android json firebase firebase-cloud-messaging

12
推荐指数
2
解决办法
7444
查看次数