Since Android Jelly Bean it has been possible to add up to 3 action buttons at the bottom of your notification, each launching a different PendingIntent:
Building on the example given in the Notification post:
new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) .setColor(ContextCompat.getColor(context, R.color.colorPrimary)) .setSmallIcon(R.drawable.ic_app_smallIcon) .setLargeIcon(R.drawable.ic_app_largeIcon) ... // Code clipped for brevity .addAction(firstAction); // Add 1st action, taking a Notification.Action parameter (see method below) .addAction(secondAction); // Add 2nd action, taking a Notification.Action parameter .setAutoCancel(true); // Notification automatically disappears once clicked notificationManager.notify(NOTIFICATION_ID, builder.build()); // Trigger the notification }Typical code to create the first Notification.Action method:
private static final int IGNORE_NOTIFICATION_PENDING_INTENT_ID = 3418; // Constant declared public static NotificationCompat.Action firstAction(Context context) { // Method that returns a Notification.Action object Intent intent = new Intent(context, AppIntentService.class); // Sets the Intent to an IntentService class (see below) intent.setAction(AppTasks.ACTION_DISMISS_NOTIFICATION); // Sets the action to a predefined String in another class (see below) PendingIntent pendingIntent = PendingIntent.getService(context, IGNORE_NOTIFICATION_PENDING_INTENT_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT); // IGNORE_NOTIFICATION_PENDING_INTENT_ID will be a unique ID. Flag set to CANCEL NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_app_notification, "Ignore", pendingIntent); // Sets an icon, label and PendingIntent return action; }IntentService class:
public class AppIntentService extends IntentService { public AppIntentService() { super("AppIntentService"); } @Override protected void onHandleIntent(Intent intent) { String action = intent.getAction(); // Gets the String from the .setAction in the Notification.Action method AppTasks.executeTask(this, action); // Calls a method from another class (see below) } }Additional class:
public class AppTasks { public static final String ACTION_DISMISS_NOTIFICATION = "dismiss-notification"; public static void executeTask(Context context, String action) { if (ACTION_DISMISS_NOTIFICATION.equals(action)){ // Takes the action passed in from the IntentService class and, in this case, clears all notifications NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // This code would probably be better in another class notificationManager.cancelAll(); // This code would probably be better in another class } } }ud851-Exercises-student\Lesson10-Hydration-Reminder\T10.03
