Skip to content

Rebind notification listener after device reboot#19

Open
dpeite wants to merge 1 commit into
cbeyls:mainfrom
dpeite:fix/rebind-notification-listener-after-reboot
Open

Rebind notification listener after device reboot#19
dpeite wants to merge 1 commit into
cbeyls:mainfrom
dpeite:fix/rebind-notification-listener-after-reboot

Conversation

@dpeite

@dpeite dpeite commented Jul 3, 2026

Copy link
Copy Markdown

On many devices — notably Android TV — the system fails to re-bind the NotificationListenerService after 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_COMPLETED broadcast receiver that forces the system to rebind the listener on boot:

  1. Toggling the service component off and back on (the programmatic equivalent of the manual permission toggle), which reliably triggers a rebind — including on devices where the official API alone isn't enough.
  2. Calling 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

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.
@cbeyls

cbeyls commented Jul 17, 2026

Copy link
Copy Markdown
Owner

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:

  • Do you know which Android versions are concerned by this bug? Is it something that appeared in newer versions only?
  • On Android N+, is rebinding the Service enough or do you still need to disable and enable the component before?

val component = ComponentName(context, MediaSessionListenerService::class.java)

// (1) Forceful rebind: toggle the component off and back on.
runCatching {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@cbeyls cbeyls Jul 17, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App doesn't start with device

2 participants