Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Keep notification steady at Notification Bar

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.

like image 586
Paresh Mayani Avatar asked Mar 17 '11 11:03

Paresh Mayani


2 Answers

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.

like image 112
user634618 Avatar answered Oct 05 '22 03:10

user634618


Try setOngoing(true).

notification = new Notification.Builder(getApplicationContext())
          .setLargeIcon(
                   BitmapFactory.decodeResource(getResources(),     R.drawable.ic_launcher))
          .setSmallIcon(R.drawable.ic_launcher).setTicker(tickerText)
          .setWhen(System.currentTimeMillis()).setContentTitle(contentTitle)
          .setOngoing(true)
          .setContentText(contentText)
          .setContentIntent(contentIntent)
like image 36
Himanshu Virmani Avatar answered Oct 05 '22 01:10

Himanshu Virmani