How to update Notification with RemoteViews?

后端 未结 4 614
感情败类
感情败类 2020-11-29 22:48

I\'m creating a notification with RemoteViews from a custom Service, which is running with notification in a foreground mode (that is, service will remain activ

4条回答
  •  攒了一身酷
    2020-11-29 23:28

    You would have to call NotificationManager.notify(id, notification) to let Notification System know that you want to update the notification view. Here's the docs link http://developer.android.com/training/notify-user/managing.html.

    Have a method which returns Notification object.

    private Notification getNotification(NotificationCompat.Builder mBuilder) {
        RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
        // Update your RemoteViews
        mBuilder.setContent(mRemoteView);
        Notification mNotification = mBuilder.build();
        // set mNotification.bigContentView if you want to
        return mNotification;
    
    }
    
    private void refreshNotification() {
        mNotificationManager.notify(getNotification(mNotificationBuilder),
                            NOTIFICATION_ID);
        // mNotificationBuilder is initialized already
    }
    

    Also, note that bigContentView and RemoteViews are not completely redrawn. If some of the elements of bigContentView has visibility set to GONE, and if you want to show it the next time, you have to explicitly set visibility to VISIBLE.

提交回复
热议问题