How to properly update a notification post api 11?

后端 未结 4 737
野性不改
野性不改 2021-02-02 13:14
  • Before Notification.Builder came into existence the way to update a notification that was already in the notification tray was to call setLatestEventInfo()

4条回答
  •  遇见更好的自我
    2021-02-02 14:08

    I resolved this issue by calling setWhen(0) on my Notification.Builder. It seems Android's default value for this argument doesn't suit updating bits of the notification view without the entire notification fading out / in.

    Notification.Builder builder = new Notification.Builder(c)
                    .setContentTitle("Notification Title")
                    .setSmallIcon(R.drawable.ic_notification_icon)
                    .setProgress(max_progress,current_progress,false)
                    .setWhen(0);
                    notification = builder.getNotification();
    
    mNotificationManager.notify(NOTIFICATION_ID, notification);
    

    Update:

    As WolframRittmeyer stated, using when=0 is not an elegant way. I formed a solution like following:

    if(mNotif == null) {
    //either setting mNotif first time
    //or was lost when app went to background/low memory
        mNotif = createNewNotification();
    }
    else {
        long oldWhen = mNotif.when;
        mNotif = createNewNotification();
        mNotif.when = oldWhen;
    }
    mNotificationManager.notify(NOTIFICATION_ID, mNotif);
    

提交回复
热议问题