Notification remains in status bar even after click

Asked

Viewed 164 times

2

I have an application where I receive daily notifications, however by clicking on this notification, it still stays on my bar status. I’m using the class Notificationcompat for the creation of these notifications, in which whenever another one is launched, it overrides the previous one. Below follows the code that generates the notification:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent(context, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
    context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(
    context.getResources(), R.mipmap.ic_launcher));

builder.setContentTitle("Title");

builder.setContentText("Content of notification");

NotificationManager notificationManager = (NotificationManager) context.
     getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(1, builder.build());

How do I make sure that when I click on the notification it no longer stays on the status?

1 answer

4


try as follows:

builder.setAutoCancel (true);

Also, although it is not really necessary, if you really want to use FLAG_AUTO_CANCEL, just add it:

 Notification notification=  builder.build();
 notification.flags |= Notification.FLAG_AUTO_CANCEL;
 notificationManager.notify(1, builder.build());

According to the documentation:

setAutoCancel

Make this notification automatically ignored when the user toca. The Pendingintent set with setDeleteIntent (Pendingintent) will be sent when this happens.

FLAG_AUTO_CANCEL

Flag field to be defined if the notification should be cancelled when it is clicked by the user.

SOURCE

  • But what would be the difference between setAutoCancel and FLAG_AUTO_CANCEL?

  • I already put the +1 because I had already solved here with the first option hours ago. Now I do not understand why they denied the question.

  • 3

    To negative there are several! To answer few! Hahahaha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.