iGio90
iGio90

Reputation: 3301

NotificationListenerService: detect if application is allowed to listen for notification

i saw some applications with a little dialog asking for permit the app to listen for notification. That dialog got 2 button: cancel, and go (that opens the security settings to allow apps for listen for notification). That dialog is persisten so i guess it have a sort of method to detect if the app is allowed or not. Anyone can point me to that API? Thanks

Upvotes: 2

Views: 3032

Answers (3)

Mohammed AlBanna
Mohammed AlBanna

Reputation: 2406

I know this is an old question, but here what I use now in my application:

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!
}else{
      //Your application has access to the notifications
}

You can move the user to Notification Access Permission settings by open the activity:

startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")); 

This is tested very well from Jelly Bean 4.3 to Marshmallow 6.0 and I use it in my applications.

Upvotes: 4

Salvatore Martire
Salvatore Martire

Reputation: 21

Hi I don't think that there is a method to call to know if you have permission to Listen Notifications, but you can try the following:

  1. Try to acquire the reference of your NotificationListenerService instance.
  2. Now if you got a null pointer when you expected it to be not null then you should prompt a Dialog asking user to enable the Security setting.
  3. add onClickListener in "Ok" button and now just startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));

Upvotes: 1

Booger
Booger

Reputation: 18725

All applications on Android can send notifications, there is not even a Permission for it. Any dialog you see in UIs is something implemented by each developer (to be extra considerate).

Bottom line, there is no API for accessing if an app can send notifications (all can).

Otherwise, there are Application Permissions for a variety of other things, which would also be worth learning about.

Upvotes: -4

Related Questions