Notification remains in the status bar even after the click

2

I have an app where I get daily notifications, however when I click this notification, it still stays in my status bar . I'm using the NotificationCompat class to create these notifications , in which whenever another is thrown, it even overlaps the previous one. Here is 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 click the notification button to stop it from being in the status bar ?

    
asked by anonymous 14.03.2017 / 02:40

1 answer

4

try the following:

builder.setAutoCancel (true);

Also, although it's 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

  

Automatically ignore this notification when the user   touches The PendingIntent set with setDeleteIntent (PendingIntent)   will be sent when this happens.

FLAG_AUTO_CANCEL

  

Flag field that should be defined if the notification should be   canceled when it is clicked by the user.

SOURCE

    
14.03.2017 / 03:27