drmgg4411 2017-02-17 06:23
浏览 79
已采纳

在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");
 }
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?