我需要使用Firebase的不可折叠通知。为此,我使用这样的数据消息:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
此消息在onMessageReceived()方法中解释。 我仍然希望在托盘中显示此数据消息,就像系统自动显示通知消息一样。
如何实现这一目标?我找不到相关文档。
谢谢!
答案 0 :(得分:4)
您可以通过调用RemoteMessage.getData()
,然后onMessageReceived()
,在.get("<KEY_HERE>")
中检索数据有效内容中的值。像这样:
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "onMessageReceived()");
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
String dataTitle = remoteMessage.getData().get("data_title");
String dataBody = remoteMessage.getData().get("data_body");
sendNotification(dataTitle, dataBody);
}
你必须自己build and display the Notification。像这样:
private void sendNotification(String title, String body) {
Notification notif = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notif);
}