我正在使用Firebase生成通知,假设如果用户单击任意一个通知,所有通知都消失了,则会有3条通知,这里我需要的是,当用户单击一个通知而不是其他两个通知时,此处的通知应该是我完成的代码。
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);
答案 0 :(得分:0)
有关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用作{{1} }-对于Integer
,您可以只输入PendingIntent
,对于0
,我建议您设置一些manager.notify
并在每个已发布的通知中增加它,因此下一个将有{ {1}}(例如,使用int id = 0;
来存储上次使用的id = prevNotificationId + 1
)