How to Clear a Notification in Android

How to clear notifications when the app is running?

The simplest way to clear notification bellow you can use it in your case where you need, Just you need to remember you notification id which you defining for clear specific notification otherwise you can clear all.

Specific notification

public void clearNotofication(int notificationId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) this.getSystemService(ns);
nMgr.cancel(notificationId);
}

Cancel All

just replace nMgr.cancel(notificationId); to nMgr.cancelAll(); from Above code.

Android: remove notification from notification bar

This is quite simple. You have to call cancel or cancelAll on your NotificationManager. The parameter of the cancel method is the ID of the notification that should be canceled.

See the API: http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

How to properly clear all notification once clicked?

My solution is to call it at onResume().

@Override
protected void onResume() {
super.onResume();

// Clear all notification
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
}

How to remove notification from notification bar programmatically in android?

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

How to remove old notification channels?

Notification channels stay forever once they are created.

To remove them again, simply call deleteNotificationChannel() on the NotificationManager with the ID of the channel you want to delete:

notificationManager.deleteNotificationChannel("channel_id");

How to clear all notification without clicking on notification in android

If you want to just clear Notifications according to id's do it this way,

while notifiy notification from NotificationManager set a tag to your nofification's and save id's of notification's in a Arraylist like this

static ArrayList<String>notifIds=new ArrayList<>();

//your code


notificationManager.notify("myappnotif",NOTIFICATION_COUNT, builder.build());
notifIds.add(data);

now to remove Notifications call notificationManager.cancel(String tag, int id) wherever you want like this

  NotificationManager notificationManager = (NotificationManager)  getSystemService(NOTIFICATION_SERVICE);
for(int i=0;i<MainActivity.notifIds.size();i++){
notificationManager.cancel("myappnotif", i);
}

and to remove all Notification's when application start's call notificationManager.cancelAll(); in onCreate of Launcher Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}

Remove all notifications from notification bar

Starting with API Level 18 you can cancel Notifications posted by other apps different than your own using NotificationListenerService, the method changed a little in Lollipop, here is the way to remove notifications covering also Lillipop API.

First inside the onNotificationPosted method you store all the StatusBarNotification objects.
Then you should maintain an updated reference to those objects, removing them if the notification is somehow dismissed in onNotificationRemoved method.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Store each StatusBarNotification object
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// Delete removed StatusBarNotification objects
}
}

Finally you can remove any notification using cancelNotification method.

 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
cancelNotification(sbn.getKey());
}

Clear notification on Action Button click

To close the notification when the "Sluiten" link is clicked on the notification I have included a working version of code.

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

final int NOTIFICATION_ID = 1;
NotificationManager notificationManager;
Button notificationBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(getIntent()!=null && getIntent().hasExtra(getPackageName())){
notificationManager.cancel(NOTIFICATION_ID); //closes notification
}
notificationBtn = (Button) findViewById(R.id.notificationBtn);
notificationBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.notificationBtn:
showNotification();
break;
}
}

void showNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_account_box_black_24dp);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Geschiedenis \nFrans \nDuits \nNatuurkunde \nWiskunde"));
builder.setContentTitle("Rooster: ");
builder.setOngoing(true);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
Intent showIntent = new Intent(this, MainActivity.class);
showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Sluiten", contentIntent);
builder.setContentIntent(contentIntent);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}


}

NOTE
In the previous answer I did not include the line

showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()

which is essential to closing the notification on click



Related Topics



Leave a reply



Submit