'CompanionDeviceManager requestNotificationAccess fails on Android 12
I am using CompanionDeviceManager to connect to a custom built BLE smart watch. This works fine, and after the relationship is established, I want to obtain Notification Listener permission so I can forward notifications to the watch. I do this using a new Android API like this:
val deviceManager = this.getSystemService(Context.COMPANION_DEVICE_SERVICE) as CompanionDeviceManager
deviceManager.requestNotificationAccess(ComponentName(applicationContext, MyNotificationListenerService.javaClass))
The above pops a dialog as expected, and when I tap the button to grant the permission, the dialog disappears and when I call deviceManager.hasNotificationAccess(ComponentName(applicationContext, MyNotificationListenerService.javaClass)), it returns true.
So far so good! This works great on Android 11 and I can successfully get callbacks about notifications.
The problem comes on Android 12. While the NotificationListenerService gets started, it never gets any callbacks for notifications. When I check the Notification forwarding permissions in Settings, I see this:
Only if I manually bring up that settings pane and check those two checkboxes, then I start getting the callbacks for notifications successfully.
Is there any way to configure this so those two checkboxes are checked by default on Android 12? Otherwise there is no point whatsoever in using deviceManager.requestNotificationAccess, right?
My AndroidManifest.xml has this:
<uses-feature android:name="android.software.companion_device_setup" android:required="true"/>
<uses-permission android:name="android.permission.REQUEST_COMPANION_PROFILE_WATCH" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND" />
<uses-permission android:name="android.permission.REQUEST_COMPANION_USE_DATA_IN_BACKGROUND" />
<uses-permission android:name="android.permission.REQUEST_OBSERVE_COMPANION_DEVICE_PRESENCE" />
...
<service android:name=".MyNotificationListenerService"
android:exported="true"
android:label="Watch Notification Forwarding"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
<meta-data
android:name="android.service.notification.default_filter_types"
android:value="conversations,alerting">
</meta-data>
<meta-data
android:name="android.service.notification.disabled_filter_types"
android:value="ongoing,silent">
</meta-data>
</service>
Assuming this is an Android 12 bug I opened an issue on the Android issue tracker here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


