FCM 推送通知即使在实现 dats 消息后也会显示原始 json



以下是我尝试从Firebase控制台发送的json。

{
"message": {
"token": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data": {
"Nick": "Mario",
"body": "great match!",
"Room": "PortugalVSDenmark"
}
}
}

我在 onMessageReceived 中的代码如下

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
String name = remoteMessage.getData().get("Nick").toString();
String body = remoteMessage.getData().get("body").toString();
String room = remoteMessage.getData().get("Room").toString();
String messageBody = name
+ " "
+ body
+ " "
+ room;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Zone")
.setContentText(messageBody)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
/*Notification with Image*/;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

每次我从Firebase控制台发送上述json时,它甚至没有输入if条件,因为remoteMessage.getData((.size((不大于零。

为什么在我发送数据有效负载时会发生这种情况?

同样,当收到通知时,我想从原始 json 创建一个句子并显示它。例如"尼克喜欢一张照片">

当通知通过时,仅显示 json。

为此苦苦挣扎了很长时间.

任何帮助将不胜感激。谢谢

试试这个。

String nick,body,room, fullString;
if(!remoteMessage.getData().isEmpty()){
Log.d(TAG, "Notification Message Data: " + remoteMessage.getData());
nick=remoteMessage.getData().get("Nick");
body=remoteMessage.getData().get("body");
room=remoteMessage.getData().get("Room");
Log.d(TAG, "All data: " + nick+","+body+","+room);
fullString = "Anything you want"+nick+"anything";
//then use the string anywhere
}

相关内容

  • 没有找到相关文章

最新更新