Notification.Builder add action
Your code in getPendingAction()
will always return the same PendingIntent
. You are not creating a separate PendingIntent
each time you call this method. To ensure that each call creates a separate PendingIntent
, you need to make the Intent
unique. You can do this by setting the ACTION in the Intent
, like this:
intent.setAction(action.name());
To ensure that any old PendingIntent
s with the same ACTION are overwritten by the latest extras, I would also call getBroadcast()
like this:
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Comments
-
Jesus Dimrix about 2 years
I'm trying to know which button is pressed so I do this on onReceive
Log.e(TAG, "Clicked " + extras.getInt("ACTION"));
and I always, no matter which button I press, get 3
(ActionEnum.GO_TO_REMINDERS)
which is thesetContentIntent
.another issue is that the notification is not closed unless I press on the notification buddy, but it's not closed when I press the button.
public void createNotification(Context context, Reminder reminder) { // Build notification Notification noti = new Notification.Builder(context) .setContentTitle(reminder.getDisplayString()) .setContentText("Pick Action") .setSmallIcon(R.drawable.icon_remider) .setContentIntent( getPendingAction(context, reminder, ActionEnum.GO_TO_REMINDERS)) .addAction(R.drawable.icon, "Take", getPendingAction(context, reminder, ActionEnum.TAKE)) .addAction(R.drawable.icon, "Snooze", getPendingAction(context, reminder, ActionEnum.SNOOZE)) .addAction(R.drawable.icon, "Remove", getPendingAction(context, reminder, ActionEnum.REMOVE)) .build(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); // hide the notification after its selected noti.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, noti); } public PendingIntent getPendingAction(Context context, Reminder reminder, ActionEnum action) { // Prepare intent which is triggered if the // notification is selected Intent intent = new Intent(context, RemindersReceiver.class); intent.putExtra("ID", reminder.getIntId()); intent.putExtra("CLICK", true); intent.putExtra("ACTION", action.getValue()); Log.e(TAG, "set action : " + action.getValue()); return PendingIntent.getBroadcast(context, 0, intent, 0); }
-
Kevin Lee over 8 yearsA tiny tip to add: if you're working with multiple pending intents and things don't seem to work even after you have made changes like changing the flags, try restarting your debugging device, because Android may have cached the old pending intents.
-
chillNZ almost 3 yearsThank you this fixed my issue. Why do we need these magic methods like setAction that subtly break things if you don't use them 🤦♂️just enforce it in the constructor or something, and people can supply the same action if they want it to be re-used... so many minefields to walk through to get Android to work correctly.
-
David Wasser almost 3 yearsThe Android
Intent
system is very powerful, but also very complex. All frameworks have their faults. Just be happy you aren't coding for iOS ;-)