android - 单击通知时,所有其他通知也消失了
问题描述
我正在使用 Firebase 生成通知,假设有 3 个通知,如果用户点击任何一个所有通知都消失了,我需要的是当用户点击一个通知而不是其他 2 个通知时应该在通知中这是我的代码 wt我已经做好了。
Intent intent;
if (pref.getString("id","").equals("")){
intent = new Intent(this, Login_Activity.class);
}else {
intent = new Intent(this, TicketListActivity.class);
}
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
String msgBody = remoteMessage.getData().get("body");
String ticketid = remoteMessage.getData().get("ticket_Id");
Notification notification = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.app_launch)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body")).setAutoCancel(true).setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get("body")))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setOngoing(true)
.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
// manager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notification);
manager.notify((int) System.currentTimeMillis(), notification);
解决方案
一些关于PendingIntent.FLAG_ONE_SHOT
指示此 PendingIntent只能使用一次的标志。用于 getActivity(Context, int, Intent, int)、getBroadcast(Context, int, Intent, int) 和 getService(Context, int, Intent, int)。
如果设置,则在调用 send() 后,它将自动为您取消,并且将来通过它发送的任何尝试都将失败。
尝试将此标志更改为FLAG_UPDATE_CURRENT
也避开这条线
(int) System.currentTimeMillis()
currentTimeMillis
总有一天它long
可能会抛出异常 - 是的,我知道我们还有很多时间直到这一刻,但仍然......使用“真实” id Integer
- 因为PendingIntent
你可以放0
,因为manager.notify
我建议你设置一些int id = 0;
并增加它与每个发布的通知一起,所以每个下一个都会有id = prevNotificationId + 1
(例如SharedPreferences
用于存储上次使用的id
)
推荐阅读
- networking - 输入域名时 ERR_CONNECTION_REFUSED 但输入公网 IP 地址时 OK
- node.js - 当开发人员在多个系统上时,为 NPM 脚本放置什么?
- firebase - 应用程序进入后台时可扩展的 FCM 通知模型
- python - 包含点积的函数的 np.vectorize
- c - 字符串数组。编译器错误。也许内存超出范围?
- scala - 播放框架/Akka Streams:检测 WebSocket 何时关闭
- angular - 如何确认 Angular App Proxy 工作正常 (v7)
- c# - 有没有办法使用 bool 来检测 foreach 循环中的错误值,这样我的方法就不会执行?
- c# - 仅提取某些索引
- google-chrome - Chrome 嵌入 SVG 会导致无限请求