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.