Android - Keep notification steady at Notification Bar
Asked Answered
B

2

11

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.

Bill answered 17/3, 2011 at 11:40 Comment(4)
Are you sure keeping that notification steady is a good idea?Nessa
@Heiko Rupp sorry to say yes but its the actual requirement that i have to implement.Bill
Readers should be clear on this: using this for advertising your app will annoy your users intensely and it should only be used for were you need to provide consistent quick access to control a running service. Eg: a music app (only while something is playing), navigation (only while it's navigating a route) or GPS logger (while it's logging).Stonefly
@couling agree with you, many advertising platform use this functionality and users are being interfered.Bill
P
26

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.

Polish answered 17/3, 2011 at 12:10 Comment(0)
A
14

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)
Athanasian answered 11/8, 2013 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.