Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get permission BIND_NOTIFICATION_LISTENER_SERVICE for NotificationListenerService

Tags:

android

I created an app and added a java class that extend `NotificationListenerService'. Everything should work fine, but I just can't get to permission BIND_NOTIFICATION_LISTENER_SERVICE. I added it on the manifest, but when I check for the permission with:

        if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE)
            != PackageManager.PERMISSION_GRANTED)
    {
        Log.d(TAG, "permission denied");
    }
    else
        Log.d(TAG, "permission granted");

but I keep getting permission denied. I know that this permission isn't a "dangerous permission" so I don't have to ask for it from the user. In the manifest I declared this:

        <service android:name=".PcNotification"
        android:label="PCNotificationService"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

and it still doesn't work. I also implemented the function onListenerConnected but it never called, so it means that my service never gets connected to the notification manager, probably because I don't have the permission BIND_NOTIFICATION_LISTENER_SERVICE

I just want to know how to grant this permission to my app.

like image 791
Eldar Azulay Avatar asked Jun 16 '17 20:06

Eldar Azulay


1 Answers

I just can't get to permission BIND_NOTIFICATION_LISTENER_SERVICE

You do not need to request that permission. You need to defend your service with that permission. You are doing that in the <service> element via the android:permission attribute.

I know that this permission isn't a "dangerous permission" so I don't have to ask for it from the user.

Then get rid of the code that is requesting it from the user. It is unnecessary, and it will not work.

like image 190
CommonsWare Avatar answered Sep 28 '22 08:09

CommonsWare