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.

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题