在onMessageReceived方法中从FCM获取RemoteMessage的值

时间:2016-09-01 05:30:22

标签: android firebase-cloud-messaging

为什么remoteMessage.getData()在我的通知类中变为空? 不知道那里有什么问题,请帮帮我!!

Bundle[
    {
        google.sent_time=1472704247501,
        gcm.notification.e=1,
        gcm.notification.title=Testpush,
        from=491403630981,
        google.message_id=0: 1472704247506889%9831a5799831a579,
        gcm.notification.body=Thisisatestnotificationfromthewebapi,
        gcm.notification.data={
            'order_id': '795'
        },
        collapse_key=client
    }
]
public void onMessageReceived(RemoteMessage remoteMessage) {
         Log.w("fcm", "received notification");
         Log.e("dataChat", remoteMessage.getData().toString());

         String data="";
        try
        {
            Map<String, String> params = remoteMessage.getData();
            JSONObject object = new JSONObject(params);
            Log.e("JSON_OBJECT", object.toString());
            data=object.getString("order_id");

        }catch (Exception e){

        }
        sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),data);
    }

1 个答案:

答案 0 :(得分:0)

Try this to receive json

 public class MyGcmListenerService extends GcmListenerService {

        private static final String TAG = "MyGcmListenerService";

        /**
         * Called when message is received.
         *
         * @param from SenderID of the sender.
         * @param data Data bundle containing message data as key/value pairs.
         *             For Set of keys use data.keySet().
         */
        // [START receive_message]
        @Override
        public void onMessageReceived(String from, Bundle data) {
            String message = data.getString("message");
            String title = data.getString("title");
            String id = data.getString("id");

            //Log.d(TAG, "From: " + from);
            //Log.d(TAG, "Message: " + message);
  sendNotification(message, title);
     }
        // [END receive_message]

        /**
         * Create and show a simple notification containing the received GCM message.
         *
         * @param message GCM message received.
         */
        private void sendNotification(String message, String title) {
            Intent intent = new Intent(this, Mainscreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this/*context*/, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            notificationBuilder.setSmallIcon(R.drawable.bell);
            notificationBuilder.setContentTitle(title);
            notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
            notificationBuilder.setContentText(message);
            notificationBuilder.setAutoCancel(true);
            if (MyUtility.restoreBoolean(this, "vibrate")) {
                notificationBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000});
            }
            if (MyUtility.restoreBoolean(this, "sound")) {
                if (MyUtility.restoreNumber(this, "soundNF") == -1) {
                    notificationBuilder.setSound(defaultSoundUri);
                } else {
                    notificationBuilder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" +
                            MyUtility.restoreNumber(this, "soundNF")));
                }
            }
            notificationBuilder.setLights(Color.GREEN, 1, 1);
            notificationBuilder.setContentIntent(pendingIntent);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
        }
    }