java – Android: Using AUTO-CANCEL on a notification when your app is running in the background-ThrowExceptions

Exception or error:

I have looked at all the other AUTO-CANCEL-not-working questions here, and they all seem to involve mistakes that I am not making. I have tried both

builder.setAutoCancel(true);

and

Notification notif = builder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;

Neither works.

I am using NotificationCompat since my minimum API is 8. Here is my full code. In this particular notification, I am not calling an intent, since I don’t need the user to do anything.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getString(R.string.app_name) + ": my title");
builder.setContentText(message);
builder.setSmallIcon(R.drawable.notification_icon);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.prog_icon);
builder.setLargeIcon(bitmap);

builder.setAutoCancel(true); // dismiss notification on user click

NotificationManager notiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notiManager.notify(MY_NOTI_MANAGER_ID, builder.build());

The notification displays just perfectly. You can swipe to clear it. But simply tapping it does not dismiss the notification. It just lights up and stay there.

Some possible differences between my code and others’ posted here:
1) I am using NotificationCompat (which should not make a difference, but we’ve heard that before).
2) Since my notification is simple, I do not attach an intent.

Please let me know if you have any insights.

Edit: My purpose is to dismiss a notification without foregrounding my background app.

How to solve:

So apparently you do need a pending intent.

At Android – notification manager, having a notification without an intent, I found a solution that grabs the current active application as your pending intent (so that you don’t have to start your own activity in order to dismiss the notification).

I just added the following two lines of code (right after setting the auto-cancel):

PendingIntent notifyPIntent = 
    PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);     
builder.setContentIntent(notifyPIntent);

It worked great. I would say that if you don’t want your activity to restart as a result of the user clicking your notification, then this is your best option.

###

You appear to be missing the PendingIntent and setContentIntent() call. I believe that is required for auto-cancel to work.

Here is some Notification-displaying logic from this sample project that works:

  private void raiseNotification(Intent inbound, File output, Exception e) {
    NotificationCompat.Builder b=new NotificationCompat.Builder(this);

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
     .setWhen(System.currentTimeMillis());

    if (e == null) {
      b.setContentTitle(getString(R.string.download_complete))
       .setContentText(getString(R.string.fun))
       .setSmallIcon(android.R.drawable.stat_sys_download_done)
       .setTicker(getString(R.string.download_complete));

      Intent outbound=new Intent(Intent.ACTION_VIEW);

      outbound.setDataAndType(Uri.fromFile(output), inbound.getType());

      b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
    }
    else {
      b.setContentTitle(getString(R.string.exception))
       .setContentText(e.getMessage())
       .setSmallIcon(android.R.drawable.stat_notify_error)
       .setTicker(getString(R.string.exception));
    }

    NotificationManager mgr=
        (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    mgr.notify(NOTIFY_ID, b.build());
  }

###

Hai dear friend if you want to show non cancelable
notification(not cancelable for users) for a particular
time and after that you need clear it (like the music player) you can use this.

mNotificationBuilder .setSmallIcon(android.R.drawable.btn_plus);
mNotificationBuilder .setContentTitle("My notification");
mNotificationBuilder .setContentText("Notificattion From service");
mNotificationBuilder .setLights(0xFF0000FF, 500, 500);

Notification note = mNotificationBuilder.build();  
note.flags = Notification.FLAG_ONGOING_EVENT; // For Non cancellable notification
mNotificationManager.notify(NOTIFICATION_ID, note);

Leave a Reply

Your email address will not be published. Required fields are marked *