以下是我尝试从 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
}
关于android - 即使在实现 dats 消息后,FCM 推送通知也会显示原始 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50960368/