Reputation: 337
I'm having trouble creating a service which extends android.service.notification.NotificationListenerService
As per https://developer.android.com/reference/android/service/notification/NotificationListenerService.html I have added the following to the manifest:
<service android:name=".NotificationListener"
android:label="@string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
I override Service#onStartCommand
:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Got onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
and NotificationListenerService#onListenerConnected
:
@Override
public void onListenerConnected() {
Log.d(TAG, "Got onListenerConnected");
}
When I run, though, starting the service with an explicit intent, I see that onStartCommand
runs but not onListenerConnected
. If I try and run requestInterruptionFilter(NotificationListenerService.INTERRUPTION_FILTER_NONE)
, from onStartCommand
(which is what I want to do) I get W/NotificationListenerService[NotificationListener]: Notification listener service not yet bound.
Any idea why I'm not getting onListenerConnected ()
(and thus can't send requestInterruptionFilter
)?
This is on Lollipop 5.1.1 on Xperia Z1 compact.
Upvotes: 3
Views: 4942
Reputation: 153
To work correctly, you should grant notification access in the phone setting.
this setting is located in:
Setting > Privacy > Special permissions > Notification access.
just search Notification access in the setting.
You can use this intent:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
startActivity(Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS))
else
startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
check your app name in the list.
Upvotes: 2
Reputation: 21
You can programmatically check if your application is given the permission with the following:
String notificationListenerString = Settings.Secure.getString(this.getContentResolver(),"enabled_notification_listeners");
//Check notifications access permission
if (notificationListenerString == null || !notificationListenerString.contains(getPackageName())) {
//The notification access has not acquired yet!
Log.d(TAG, "no access");
requestPermission();
}
else {
//Your application has access to the notifications
Log.d(TAG, "has access");
}
You can then setup a notification to point the user to enable the notification permission with requestPermission()
public void requestPermission() {
Intent requestIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
requestIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(requestIntent);
}
Upvotes: 2
Reputation: 957
Android 5.1 Notification access screenshot
Upvotes: 6
Reputation: 13541
Your app has to explicitly be allowef by the user to listen to notifications. This will grant your service to run and recieve callbacks. There is no way around it.
You need to find the option somewhere in the Settings app.
Upvotes: 2