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
WARNING!
The only correct way to update notification is to recreate RemoteViews before each NotificationManager#notify. Why? There's a memory leak leading to TransactionTooLargeException, as it has been reported in those questions:
Each call on RemoteViews such as setViewVisibility(...) and so on adds corresponding action to queue of actions are to be applied. After notify the remote view is inflated and actions are actually applied. But the queue is not cleared!
Take a look at screenshot taken during debugging this case.
There I'm updating audio player notification with data coming from ViewModel. Application is stopped on line #81 and you can see the RemoteViews instance that has array of actions with size 51! But I only switched audio track twice and pressed pause! Of course I had to observe application crash with TransactionTooLargeException after a while.
Shallow research confirmed there's no public API to directly or indirectly clear actions queue, so the only way to update notification view is to hold its state separately and recreate RemoteViews instance passed to Notification.Builder, anyway this doesn't overload UI thread a lot