2

I need to show a notification and update it over the course of its lifetime. In particular, I need to change certain flags, such as "show chronometer". For that, the documentation states that we should call NotificationManager.notify() with the same id.

So the idea is to have these methods:

private void showInitialNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    ...
    mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}

private void addChronometerFlag() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    ...
    builder.setUsesChronometer(true);
    mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}

private void removeChronometerFlag() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    ...
    builder.setUsesChronometer(false);
    mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}

This works perfectly OK in Android 5.1 and 6.0, but in Android 4.4 (and below, I guess?) the chronometer is not removed, although the content title, text, icon &c are updated correctly.

Any idea why?

I cannot use the same Notification.Builder object (for example as suggested in this answer) since I also need to update the actions available on the notification and there is no way to remove previously added actions to a Builder.

My use case is somewhat more complicated (it's a media player) but this is the minimum example of the bug I've been able to reproduce. It's also present, for example, in Google's UniversalMusicPlayer sample -- works fine in Lollipop, but not in KitKat.

FWIW, the full example code is here.

Community
  • 1
  • 1
matiash
  • 54,791
  • 16
  • 125
  • 154
  • Take a look at the `Notification` built by the builder. See what's in the `extras` `Bundle`. There should be an `EXTRA_SHOW_CHRONOMETER` `boolean` value in there. See whether it lines up with your `setUsesChronometer()` value. – CommonsWare Apr 19 '16 at 22:16
  • Hi @CommonsWare - I've checked the notification object returned by the builder and the extras are OK (i.e. `android.showChronometer` is either true or false as appropriate). However it's not working properly, don't know why. – matiash Apr 20 '16 at 13:50
  • Next, try replacing `NotificationManagerCompat` with `NotificationManager`. If the problem persists, then the problem presumably resides in the system, and there's nothing that you can do about it. If the problem goes away, you are running into a bug in `NotificationManagerCompat`. – CommonsWare Apr 20 '16 at 13:56
  • @CommonsWare Unfortunately it doesn't make a difference; looks like it's a platform bug indeed. I'll just have to avoid setting these flags for pre-Lollipop... Thanks for the help. – matiash Apr 20 '16 at 14:08
  • Depending on your use case, you could perhaps `cancel()` the existing `Notification` first. That will trigger any ringtones and such again when you `notify()`, and so that may be worse than your current situation. – CommonsWare Apr 20 '16 at 14:11
  • @CommonsWare The problem is, this is a "media player control" notification, so pressing the play/pause actions would cause the notification to flicker. I've tried it and the effect is indeed worse. – matiash Apr 20 '16 at 14:15

0 Answers0