2017-05-21 27 views
0

link of structureFCM onReceivedMessage()獲得捆綁

我想從我的ArrayMap的消息,但我無法訪問ReceiveMessage束。 我試圖直接訪問地圖,但它是非常錯誤的

我的代碼

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



    } 
} 
+0

你是不是直接訪問地圖,你調用方法,它返回它。你爲什麼認爲這是錯的? – X3Btel

+0

請參閱:https://stackoverflow.com/questions/42273699/how-to-stack-firebase-cloud-messaging-notifications-when-the-application-is-not/43914028#43914028 – Maddy

回答

0

獲取使用的getJSON,現在束值,就會構建值作爲字符串JSON格式,

private String message = getJson(bundle); 

將字符串JSON來的JSONObject,

JSONObject object = new JSONObject(message); 

從對象得到像object.optInt(「NAME」);

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




} 

試試這個。它可能會幫助你。

0

感謝@Pritish喬希

我發現這個答案,幫了我很多 ,這裏是代碼片段

你在地圖的形式獲取數據

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" 
    } 
} 

參考: