【问题标题】:FCM onReceivedMessage() get BundleFCM onReceivedMessage() 获取捆绑包
【发布时间】:2017-05-21 20:31:22
【问题描述】:

link of structure

我正在尝试从我的 ArrayMap 中获取消息,但我无法访问 ReceiveMessage 包。 我尝试直接访问Map,但是非常错误

我的代码

public class FbService extends FirebaseMessagingService {
    public FbService() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.

        Map<String, String> params = remoteMessage.getData();
        String message =  params.get(3);

        Log.d("FbService", "Notification Message Body: " + message);



    }
}

【问题讨论】:

标签: android firebase firebase-cloud-messaging


【解决方案1】:

感谢@Pritish Joshi

我找到了这个答案并帮助了我很多 , 这里是代码sn -p

你以Map的形式获取数据

public void onMessageReceived(RemoteMessage remoteMessage)
        {
            Log.e("dataChat",remoteMessage.getData().toString());
            try
            {
                Map<String, String> params = remoteMessage.getData();
                JSONObject object = new JSONObject(params);
                Log.e("JSON_OBJECT", object.toString());
          }
       }

确保从服务器发送的数据格式正确,即在“数据”键中

这里是演示 Json 文件

{
  "to": "registration_ids",
  "data": {
    "key": "value",
    "key": "value",
    "key": "value",
    "key": "value"
  }
}

参考:

【讨论】:

    【解决方案2】:

    使用getJson获取bundle值,现在你将构造值作为Json格式的String,

     private String message = getJson(bundle);
    

    将字符串json转成JSONObject,

     JSONObject object = new JSONObject(message );
    

    从对象获取类似object.optInt("NAME");的值

    【讨论】:

      【解决方案3】:
      public class MyFirebaseMessagingService extends FirebaseMessagingService {
      
          private static final String TAG = "MyFirebaseMsgService";
          String title = "";
      
      
          @Override
          public void onMessageReceived(RemoteMessage remoteMessage) {
      
              send_Notification(remoteMessage);
      
          }
      
      
          private void send_Notification(RemoteMessage remoteMessage) {
      
              String notifications_text = "";
      
      
              String title = remoteMessage.getNotification().getTitle();
      notifications_text =remoteMessage.getNotification().getBody();
              Intent resultIntent = new Intent(this, SplashActivity.class);
              TaskStackBuilder TSB = TaskStackBuilder.create(this);
              TSB.addParentStack(SplashActivity.class);
              TSB.addNextIntent(resultIntent);
      
              PendingIntent resultPendingIntent = TSB.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
              NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
              nb.setSmallIcon(R.drawable.logo);
              nb.setContentIntent(resultPendingIntent);
              nb.setAutoCancel(false);
              nb.setLargeIcon(BitmapFactory.decodeResource(getResources(),
                      R.drawable.logo2));
              nb.setContentTitle(getString(R.string.app_name) + " " + title + " " + getString(R.string.str_notification));
      NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
              mNotificationManager.notify(random(), nb.build());
      
          }
      
      
          int random() {
              Random rand = new Random();
              int randomNum = 1 + rand.nextInt((100000 - 1) + 1);
              return randomNum;
          }
      
      
      
      
      }
      

      试试这个。它可能会帮助你。

      【讨论】:

        猜你喜欢
        • 2010-10-15
        • 2017-07-23
        • 1970-01-01
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-08
        • 1970-01-01
        相关资源
        最近更新 更多