Skip to content Skip to sidebar Skip to footer

Remove The Notification Icon From The Status Bar

I am displaying an icon in status bar.Now I want to remove that icon immediately when I open that content, after some time if we receive any alert, that icon will be displayed agai

Solution 1:

Use the NotificationManager to cancel your notification. You only need to provide your notification id.

https://developer.android.com/reference/android/app/NotificationManager.html

privatestaticfinalint MY_NOTIFICATION_ID= 1234;
Stringns= Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(MY_NOTIFICATION_ID, notification);

The example code is not complete. It depends on how your created your notification. Just make sure you use the same id to cancel your notification as you used when you created your notification.

To cancel:

mNotificationManager.cancel(MY_NOTIFICATION_ID);

Solution 2:

If you want to remove the notification once user clicked on it, set notification flag FLAG_AUTO_CANCEL before you create the notification.

Solution 3:

I used the Builder patter so you can just set auto cancel from the setter setAutoCancel(true). That looks something like this:

Stringtitle="Requests"; 
    Stringmsg="New requests available.";
    NotificationCompat.BuildermBuilder=newNotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_gcm_icon)
                    .setContentTitle(title)
                    .setAutoCancel(true)
                    .setStyle(newNotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

Solution 4:

IntentresultIntent=newIntent(application, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntentresultPendingIntent= PendingIntent.getActivity(application, 0, resultIntent, 0);
NotificationManagernmgr= (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.BuildermBuilder=newNotificationCompat.Builder(application)
            .setSmallIcon(R.drawable.icon_battery)
            .setContentTitle(application.getString(R.string.app_name))
            .setContentText("your text")
            .setOnlyAlertOnce(false)
            .setAutoCancel(true)
            .setTicker("your ticker")
            .setDefaults(Notification.DEFAULT_SOUND  ) //| Notification.DEFAULT_VIBRATE
            .setContentIntent(resultPendingIntent)
            .setVisibility(VISIBILITY_SECRET)
            .setPriority(Notification.PRIORITY_MIN);

NotificationmNotification= mBuilder.build();
//  mNotification.flags |= FLAG_NO_CLEAR;
nmgr.notify(0, mNotification);

Post a Comment for "Remove The Notification Icon From The Status Bar"