android - Notification not updating correctly -
i run service, configured foreground service via startforeground(int id, notification notification
), , want update notification. code achieve looks follows:
private void setforeground() { notification foregroundnotification = this.getcurrentforegroundnotification(); // start service in foreground notification this.startforeground(myservice.foreground_id, foregroundnotification); } ... private void updateforegroundnotification() { notification foregroundnotification = this.getcurrentforegroundnotification(); // update foreground notification notificationmanager notificationmanager = (notificationmanager) this.getsystemservice(context.notification_service); notificationmanager.notify(myservice.foreground_id, foregroundnotification); }
and generate notification depending on service state:
private notification getcurrentforegroundnotification() { // set notification info string contenttext = ...; // build notification if (this.mundeliveredcount > 0) { string contenttitlenew = ...; this.mnotificationbuilder .setsmallicon(r.drawable.ic_stat_notify_active) .setcontenttitle(contenttitlenew) .setcontenttext(contenttext) .setlargeicon(bitmapfactory.decoderesource(this.getresources(), r.drawable.ic_stat_notify_new)) .setnumber(this.mundeliveredcount) .setwhen(system.currenttimemillis() / 1000l) .setdefaults(notification.default_all); } else { this.mnotificationbuilder .setsmallicon(r.drawable.ic_stat_notify_active) .setcontenttitle(this.gettext(r.string.service_notification_content_title_idle)) .setcontenttext(contenttext) .setlargeicon(null) .setnumber(0) .setwhen(0) .setdefaults(0); } // generate intent intent intentformainactivity = new intent(this, mainactivity.class); pendingintent pendingintent = pendingintent.getactivity(this, 0, intentformainactivity, 0); // build notification , return this.mnotificationbuilder.setcontentintent(pendingintent); notification foregroundnotification = this.mnotificationbuilder.build(); return foregroundnotification; }
the problem notification doesn't updated correctly: when start service run in foreground, call updateforegroundnotification()
several times this.mundeliveredcount > 0
, after again this.mundeliveredcount == 0
, small notification icon in bottom right corner of notification won't disappear, though no large icon has been provided. behaviour not expected according documentation of setsmallicon(int icon)
method of notificationbuilder
class stated small icon should appear in bottom right corner if large icon has been specified:
public notification.builder setsmallicon (int icon)
set small icon resource, used represent notification in status bar. platform template expanded view draw icon in left, unless large icon has been specified, in case small icon moved right-hand side.
what doing wrong here updating service notification? or android bug?
after having decided it's not wrongdoing here leads unwanted small notification icon, searched , found simple workaround aforementioned bug:
when updating notification
via updateforegroundnotification()
method, generate , update notification id "reset" notification. "reset" notification configured follows:
this.mnotificationbuilder .setsmallicon(0) .setcontenttitle("reset") .setcontenttext("reset") .setlargeicon(null) .setnumber(0) .setwhen(0) .setdefaults(0);
with such notification, call
notification resetforegroundnotification = this.getresetforegroundnotification(); this.mnotificationmanager.cancel(myservice.foreground_notification_id); this.mnotificationmanager.notify(myservice.foreground_notification_id, resetforegroundnotification);
before performing intended notification update, , unwanted bottom-right-corner-icon gone in next notification sets small icon.
Comments
Post a Comment