Rebind notification listener after device reboot#19
Conversation
On many devices (notably Android TV) the system fails to re-bind the NotificationListenerService after a reboot, so the app silently stops receiving media session updates until the notification access permission is manually toggled off and on again. Add a BOOT_COMPLETED receiver that forces the system to rebind the listener on boot by toggling the service component and calling NotificationListenerService.requestRebind(). The notification-access grant is not affected.
|
Thank you very much for investigating this. Indeed I received a few bug reports hinting about this bug. I was planning to add a delay before retrieving the MediaSessions but I suspect this would have no effect because the root cause is the Service not binding. Before merging I want to check a few things with you:
|
| val component = ComponentName(context, MediaSessionListenerService::class.java) | ||
|
|
||
| // (1) Forceful rebind: toggle the component off and back on. | ||
| runCatching { |
There was a problem hiding this comment.
runCatching is a method to create a Result, I would rather explicitly catch the Exception if the result is ignored.
| // (2) Ask the system to rebind the listener (available since API 24). | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
| runCatching { | ||
| NotificationListenerService.requestRebind(component) |
There was a problem hiding this comment.
Is it needed to do both the disable/enable and the rebind, or one of them is enough?
Maybe the Service enable/disable can be used as a fallback on older Android versions, while newer Android versions can just rebind the service.
|
|
||
| <receiver | ||
| android:name=".service.BootReceiver" | ||
| android:exported="true"> |
There was a problem hiding this comment.
This should be set to false for security reasons. The only reason to set it to true is to receive intents from other non-system apps, but it's not necessary for android.intent.action.BOOT_COMPLETED intents.
On many devices — notably Android TV — the system fails to re-bind the
NotificationListenerServiceafter a reboot. When this happens the app silently stops publishing media session state to MQTT until the user manually toggles the notification access permission off and on again.This PR adds a
BOOT_COMPLETEDbroadcast receiver that forces the system to rebind the listener on boot:NotificationListenerService.requestRebind()(API 24+) as a final nudge.The notification-access grant is stored separately (by component name) and is not affected by the component toggle. Failures are caught defensively so the receiver never crashes on boot.
Tested on a Philips Android TV (Android 12): after a reboot the listener reconnects automatically and playback state keeps publishing without any manual intervention.
Reference: https://stackoverflow.com/a/64576489
Fixes #7 · May also address #12