and*_*ain 6 android push-notification firebase firebase-cloud-messaging
我正在开发一个应用程序,在那里我收到 FCM 通知,因为当应用程序打开时,onMessageRecieved()方法会触发,我根据标签和 ID 通知消息,并根据 ID 和标签删除通知。但是当应用程序在后台时onMesasageRecived()不会调用。当我从后台收到通知时,如何附加 id 和标签通知或如何根据某个 id 删除单个通知。
见 onMessageReceived
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
............
notificationManager.notify("tag", notificationId, notification);
.......
}
Run Code Online (Sandbox Code Playgroud)
用于删除消息
private void clearNotifications() {
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel("tag",notificationId);
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,但是当应用程序关闭时,代码不起作用。我们可以从服务器端有效负载通知或附加标签和 ID
如果您查看文档,在这里您会发现,如果您有notification有效负载,当您的应用程序位于后台时,您的通知将直接传递到系统托盘,没有办法拦截它。如果您有一个带有选项数据有效负载的通知有效负载,也会发生同样的情况,通知将直接进入托盘,数据有效负载将传递到启动器活动的意图。
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
Run Code Online (Sandbox Code Playgroud)
当您检查方法中的 id/标签时onMessageReceive,为了保证它始终调用该onMessageReceived方法,您可以做的是从通知中删除通知负载并仅添加数据负载。所有data有效负载都传递给该onMessageReceived方法。
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2435 次 |
| 最近记录: |