drmgg4411
2017-02-17 06:23
浏览 74

在json中转换fcm通知响应

Bundle
[{
    google.sent_time=1487229288769,
    gcm.notification.created_at=2017-02-16 12:44:52,
    gcm.notification.e=1,
    gcm.notification.Content_available=1,
    gcm.notification.badge=1,
    gcm.notification.title=, from=388143837768,
    google.message_id=0:1487229288775757%19aca14d19aca14d,
    gcm.notification.body=hi, 
    gcm.notification.uid=160,
    collapse_key=com.sid.Smessenger
}]

I am getting above response from fcm and I want to convert it into json please check above android code.

getting response from remoteMessage.
I am not getting data in getBody().
getting null value only.

public void onMessageReceived(RemoteMessage remoteMessage) 
{
    Log.d(TAG, "FROM: " + remoteMessage.getFrom());
    remoteMessage.getFrom();

    if (remoteMessage.getData().size() > 0) 
        {
        Log.d(TAG, "Message data: " + remoteMessage.getData());
        }
    if (remoteMessage.getNotification() != null) 
       {
        Log.d(TAG, "Message body:" +  
        remoteMessage.getNotification().getBody());

        String questionId =  
        remoteMessage.getData().get("gcm.notification.gid");
        String userId = 
        remoteMessage.getData().get("gcm.notification.uid");
        Log.e("Group ID", questionId);
        Log.e("U ID", userId);

        Bundle bundle = new Bundle();
        for (Map.Entry<String, String> entry :
        remoteMessage.getData().entrySet()) {
            bundle.putString(entry.getKey(), entry.getValue());
            Log.e(entry.getKey(), entry.getValue());

            //run it/// and get notification///
        }
        // String created_at = remoteMessage.
        sendNotification(remoteMessage.getNotification().getBody());
        }

    // String value = bundle.getString("request");
 }

图片转代码服务由CSDN问答提供 功能建议

  Bundle 
 [{
 google.sent_time = 1487229288769,
 gcm.notification.created_at =  2017-02-16 12:44:52,
 gcm.notification.e = 1,
 gcm.notification.Content_available = 1,
 gcm.notification.badge = 1,
 gcm.notification.title =,  from = 388143837768,
 google.message_id = 0:1487229288775757%19aca14d19aca14d,
 gcm.notification.body = hi,
 gcm.notification.uid = 160,
 collapse_key = com.sid.Smessenger 
}] \  n   
 
 

我从fcm获得以上响应,我想将其转换为json 请检查以上android代码。

从remoteMessage获取响应。
我没有在getBody()中获取数据。
ngetting null value only。

   public void onMessageReceived(RemoteMessage remoteMessage)
 {
 Log.d(TAG,“FROM:”+ remoteMessage.getFrom()); 
 remoteMessage.getFrom(); 
 
 if(remoteMessage.getData  ()。size()&gt; 0)
 {
 Log.d(TAG,“消息数据:”+ remoteMessage.getData()); 
} 
 if(remoteMessage.getNotification()!= null)  
 {
 Log.d(TAG,“消息正文:”+ 
 remoteMessage.getNotification()。getBody()); 
 
字符串questionId = 
 remoteMessage.getData()。get(“gcm。  notification.gid“); 
 String userId = 
 remoteMessage.getData()。get(”gcm.notification.uid“); 
 Log.e(”Group ID“,questionId); 
 Log.e(  “U ID”,userId); 
 
 Bundle bundle = new Bundle(); 
 for(Map.Entry&lt; String,String&gt; entry:
 remoteMessage.getData()。entrySet()){
 bundle  .putStrin  g(entry.getKey(),entry.getValue()); 
 Log.e(entry.getKey(),entry.getValue()); 
 
 //运行///并获取通知//  / 
} 
 // String created_at = remoteMessage。
 sendNotification(remoteMessage.getNotification()。getBody()); 
} 
 
 //字符串值= bundle.getString(“request”); \  n} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyong1885 2017-02-17 06:28
    已采纳

    From your response snippet, I believe your talking about how should you do it on Android.

    It is in JSON but wrapped in your data bundle, that you have received as argument in FCM receiver function try getting values from your bundle, as usually we do in android.

    Rather than thinking "JSON", think "key/value pairs". Your server sends data in key/value pairs. Your app receives that data as key/value pairs in the extras in the Intent you get. You know what your keys are, so just retrieve the values out of the extras that are tied to those keys (e.g., getStringExtra("message"), getStringExtra("title"))

    Hope this solves your prob.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题