2017-05-21 24 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" 
    } 
} 

参考: