First, press-and-hold on the persistent notification you want to remove. Another option is to swipe the notification left or right, and then tap on the cogwheel icon shown next to it. Next, tap on the switch next to Permanent to disable it, and then press Save.
You can directly add . setAutoCancel(true) this line into your code in order to remove notification on click.
While building Notification
by NotificationBuilder
you can use notificationBuilder.setAutoCancel(true);
.
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL
From the documentation:
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user
// Uses the default lighting scheme
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Will show lights and make the notification disappear when the presses it
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
2016 state: you can use mBuilder.setAutoCancel(true)
.
Source: https://developer.android.com/reference/android/app/Notification.Builder.html
The answer for me was to use mBuilder.setOngoing(false)
Use the flag Notification.FLAG_AUTO_CANCEL
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
and to launch the app:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, App.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notifications remain visible until one of the following happens:
For more details see: https://developer.android.com/training/notify-user/build-notification?hl=en
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With