Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Dismiss/Cancel the status bar notification in android programmatically

I have created a status bar notification in android programmatically using the code given below with a particular id

    Notification notification;     public static NotificationManager myNotificationManager;     public static final int NOTIFICATION_ID = 1;  private static void createStatusBarNotification(Context context,CharSequence NotificationTicket, CharSequence NotificationTitle,CharSequence NotificationContent, int icon) {             myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);             long when = System.currentTimeMillis();             notification = new Notification(icon, NotificationTicket, when);             Intent notificationIntent = new Intent(context, MyActivity.class);             PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);             notification.setLatestEventInfo(context, NotificationTitle,NotificationContent, contentIntent);             notification.flags |= Notification.FLAG_ONGOING_EVENT;             myNotificationManager.notify(NOTIFICATION_ID, notification);          } 

Now what i want is remove this notification on clicking of a button in tha application programatically.

How can i achieve this ?

like image 715
Piyush Agarwal Avatar asked Feb 12 '13 14:02

Piyush Agarwal


People also ask

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.


2 Answers

You can use this :

public void clearNotification() {     NotificationManager notificationManager = (NotificationManager) mContext             .getSystemService(Context.NOTIFICATION_SERVICE);     notificationManager.cancel(NOTIFICATION_ID); } 
like image 164
Gaurav Arora Avatar answered Sep 22 '22 12:09

Gaurav Arora


To cancel all

myNotificationManager.cancelAll();

like image 27
akshay Avatar answered Sep 24 '22 12:09

akshay