以下是我试图从 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。
很长一段时间以来一直在为此苦苦挣扎。
任何帮助将不胜感激。谢谢