Android - Keep notification steady at Notification Bar

Paresh Mayani picture Paresh Mayani · Mar 17, 2011 · Viewed 13.8k times · Source

I have written the function to notify and display at Notification bar:

private void showNotification()
    {
            CharSequence title = "Hello";
            CharSequence message = "Notification Demo";

            NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.icon, "A Notification", System.currentTimeMillis());
            Intent notificationIntent = new Intent(this, Main_Activity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            notification.setLatestEventInfo(Main_Activity.this, title, message, pendingIntent);
            notificationManager.notify(NOTIFICATION_ID, notification);
    }

Its working fine, But what way we can keep the notification steady at Notification bar even when user press the "clear" notificatios button from the notification bar ?

Please have a look at the "Yahoo" application's notification bar.

enter image description here

I have gone through this SDK article: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating , but failed to find out.

Answer

user634618 picture user634618 · Mar 17, 2011

Use FLAG_NO_CLEAR

Just set it on your Notification instance before supplying it to the NotificationManager:

notificaton.flags |= Notification.FLAG_NO_CLEAR;

edit: I just noticed though that if you are using Notification.Builder (since Honeycomb) and make the notification "ongoing", it will also become immune to "clear all". See here.

Appearently, this is supposed to discourage developers from using the FLAG_NO_CLEAR on a non-ongoing notificaton since this could confuse the user.