Updating an ongoing notification quietly

I too experienced this issue, and with help of previous comments and a bit of digging I have found the solution.

If you do not want notifications to flash when updated, or to continuously hog the status bar of the device,you must:

  • Use setOnlyAlertOnce(true) on the builder
  • Use the SAME Builder for each update.

If you use a new builder each time, then I am guessing Android has to rebuild the view all over again causing it to briefly disappear.

An example of some good code:

class NotificationExample extends Activity {

  private NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
  private mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  //Different Id's will show up as different notifications
  private int mNotificationId = 1;    

  //Some things we only have to set the first time.
  private boolean firstTime = true;

  private updateNotification(String message, int progress) {
    if (firstTime) {
      mBuilder.setSmallIcon(R.drawable.icon)
      .setContentTitle("My Notification")
      .setOnlyAlertOnce(true);
      firstTime = false;
    }
    mBuilder.setContentText(message)
    .setProgress(100, progress, true);

    mNotificationManager.notify(mNotificationId, mBuilder.build());
  }
}

With the above code, you can just call updateNotification(String, int) with a message and progress (0-100) and it will update the notification without annoying the user.


You should update existing notification https://developer.android.com/training/notify-user/build-notification.html#Updating