I'm creating an android java app that contains a part creates notifications to be confirmed or denied by the user, and the notifications cannot be dismissed by the user, only via the app
The problem is that once I create the notification, the action buttons work perfectly, but if I create another notification, the old notifications' buttons don't work anymore
Which causes that if the user tried to confirm or deny any old notification (via their button 'Confrm' & 'Deny'), the actions that the button must perform don't happen, but only happen with the last(newest) notification
So my question is how to keep the old notifications' buttons active after creating a new notification
I use the code below (with changing the NotificationID)
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mbuilder = new Notification.Builder(MainActivity.this);
mbuilder.setContentTitle("Confirmation");
mbuilder.setContentText("Do you want to confirm that you want to wake up at 7 am?");
mbuilder.setSmallIcon(R.drawable.app_icon);
mbuilder.setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mbuilder.setOngoing(true);
mbuilder.setAutoCancel(false);
Intent NotificationIntent = new Intent(getApplicationContext(), MainActivity.class);
NotificationIntent.setClass(getApplicationContext(), MainActivity.class);
NotificationIntent.putExtra("NotificationID", String.valueOf((long)(NotificationID)));
NotificationIntent.setType("CONFIRMATION=true");
PendingIntent PendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 1, NotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mbuilder.addAction(R.drawable.app_icon, "CONFIRM", PendingIntent1);
NotificationIntent.setType("CONFIRMATION=false");
PendingIntent PendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 1, NotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mbuilder.addAction(R.drawable.app_icon, "DENY", PendingIntent2);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("1", "Confirmations", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.setLightColor(Color.BLUE);
channel.setShowBadge(true);
channel.enableVibration(true);
mbuilder.setChannelId("1");
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(channel);
}
} else {
mbuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
}
if (mNotificationManager != null) {
mNotificationManager.notify((int)(NotificationID), mbuilder.build());
}
I change the intent type to know the clicked button because the intent can contain only one type, but if I add a flag instead, it will duplicate which will cause a problem in recognizing which button is clicked after launching the MainActivity, so it's easier to me to do that.
Thanks