8

I'm adding two action buttons to my notification, when I click either of them they perform the action desired however the notification remains in my notification drawer. I know it's possible to remove the notification from the notification drawer when an action button is clicked as that is how Gmail functions. If I click the main notification it opens the app and removes the notification from the notification drawer.

Here is a snippet of my code:

Intent completeIntent = new Intent(getApplicationContext(), MarkComplete.class);
        completeIntent.putExtra("rowid", inrowid);
        completeIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

PendingIntent markCompleteIntent = PendingIntent.getActivity(getApplicationContext(), inrow, completeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Title")
                .setContentText("text")
                .setContentIntent(notificationReleaseIntent)
                .setPriority(Notification.PRIORITY_HIGH)
                .setAutoCancel(true)
                .addAction(R.drawable.complete, "Mark Complete", markCompleteIntent);

Edit - As zionpi pointed out I needed to call notification.cancel(); to remove the notification once the addAction button had been clicked. I simply added this method to my classes where the PendingIntent was pointing.

public static void CancelNotification(Context ctx, int notifyId) {
    String  s = Context.NOTIFICATION_SERVICE;
    NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
    mNM.cancel(notifyId);
}
marty331
  • 423
  • 5
  • 12

1 Answers1

7

So you want to remove your notification, the preliminary is to have a notification id.
then invoke method similar as below to eliminate it.

 public static void CancelNotification(Context ctx, int notifyId) {
        String  s = Context.NOTIFICATION_SERVICE;
        NotificationManager mNM = (NotificationManager) ctx.getSystemService(s);
        mNM.cancel(notifyId);
    }

You may want to refer to this and this post alternatively.

Community
  • 1
  • 1
zionpi
  • 2,593
  • 27
  • 38
  • Suppose my addAction opens the MainActivity intent, then if I call this `mNM.cancel(notifyId);` in the start of MainActivity it will always cancel the notification when I open my MainActivity even if I don't click or check the notification. So when should I call this so that it will be only called after clicking the addAction button. @zionpi – Sp4Rx Nov 02 '16 at 06:43
  • Notifications remain visible until one of the following happens,refer [this](https://developer.android.com/training/notify-user/managing.html#Removing). @Sp4Rx – zionpi Nov 02 '16 at 08:08
  • That is not my question. Suppose my addAction button opens MainActivity. So to cancel the notification after clicking addAction button should I call notification.cancel(id) at the start of MainActivity? @zionpi – Sp4Rx Nov 02 '16 at 10:31
  • @Sp4Rx give it a try and see what happened. – zionpi Nov 03 '16 at 01:05
  • Yeah I have tried, in that case , the notification will be cancelled every time I open main activity, not by clicking addAction button. Are you getting my point. @zionpi – Sp4Rx Nov 03 '16 at 05:29
  • @Sp4Rx then remove relevant code at the start of MainActivity and put it right inside addAction button onClickListener. – zionpi Nov 03 '16 at 07:43