user2638180 user2638180 - 17 days ago 7
Android Question

Deleting notification from Android notification bar

I'm using the following to code to send a notification:

NotificationCompat.Builder builder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Title")
.setContentText("Message");
int NOTIFICATION_ID = 12345;

Intent targetIntent = new Intent(getBaseContext(), MainActivity4.class);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());


The code works as I expected except for one little thing, once I go to the notification bar, expand it and choose the notification, it keeps staying on the notification bar, opposite to the behavior of most notifications, than once you tap them they disappear from being there.

Guess it must be some option in the configuration of the notification but as much as I search I cannot find it.

Hope you can help.

Answer

Are you looking for this?

.setAutoCancel(true)

so you would have

NotificationCompat.Builder builder =
                    new NotificationCompat.Builder(getBaseContext())
                            .setSmallIcon(R.drawable.notification_icon)
.setAutoCancel(true)
                            .setContentTitle("Title")
                            .setContentText("Message");