سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top