2016-09-01 3 views
2

Почему remoteMessage.getData() подходит к нулю в моем классе уведомлений? Не знаю, в чем проблема, пожалуйста, помогите мне!Получить значение из RemoteMessage из FCM в методе onMessageReceived

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); 
    } 

ответ

0

Попробуйте получить 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()); 
     } 
    } 
+0

FCM принимать параметр RemoteMessage в onMessageReceived() –

+0

есть вы получите файл конфигурации из консоли firbase? –

+0

... да, я получил это –

Смежные вопросы