Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove notification from notification bar programmatically in android?

Anybody have idea how can we remove notification from application programmatically which is called using Pending intent.

I have used to cancel notification using following method.

AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(Display.this, TwoAlarmService.class); PendingIntent pi = PendingIntent.getBroadcast(Display.this, AlarmNumber, intent, PendingIntent.FLAG_CANCEL_CURRENT); am.cancel(pi); 

But problem is notification which fired already that are not removed from notification bar.

Thanks in advance...

enter image description here

like image 565
Jayeshkumar Sojitra Avatar asked Oct 09 '13 09:10

Jayeshkumar Sojitra


People also ask

How do I remove items from my notification bar?

any options you want to remove from the notification bar. Tap on any of the switches to turn them on or off. This removes these options from the notification bar.

How do I get rid of notifications on Android?

On the “Settings” menu, tap the “Sound & Notification” option, and then scroll down until you see the “App notifications” entry. Tap that. Tap each app to see its notification options. To disable notifications for an app, switch the “Block All” toggle the on position.

How do I enable and disable notifications in Android programmatically?

It is not possible to disable notifications from other apps. you can only control notifications generated by your own app. Android: is it possible to remove a system-managed notification programmatically? Show activity on this post.

How do I turn off unremovable notifications on Android?

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.


2 Answers

Maybe try this :

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(NOTIFICATION_ID); 

OR, you can also do this to cancel all notifications in given context:

notificationManager.cancelAll(); 

See this link to the documentation : NotificationManager

like image 106
An-droid Avatar answered Oct 18 '22 22:10

An-droid


I believe the most RECENT and UPDATED way of doing (Kotlin and Java) this should be done as:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID) 

Or to cancel all notifications is:

NotificationManagerCompat.from(context).cancelAll() 

Made for AndroidX or Support Libraries.

like image 26
Xenolion Avatar answered Oct 18 '22 22:10

Xenolion