9

I'm currently developing an app for Android that uses the NotificationListenerService, which requires that the user will enable notification access for my app under Setting -> Security -> Notification Access.

My question is that can I redirect the user to this place so they will enable it? So far I only managed to direct them to Setting -> Security window.

Also, is it possible to first check if the user enabled notification access for my app already and only then redirect them?

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Gershon Papi
  • 5,008
  • 3
  • 24
  • 50

3 Answers3

30

You can open the NotificationAccessSettingsActivity by using the following Intent, but I'm not sure about checking to see if they've already enabled your app.

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

Alternatively, for API 22+:

startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
adneal
  • 30,484
  • 10
  • 122
  • 151
  • Thanks a lot! If someone knows if I can find out if the user enabled my app I'd appreciate it – Gershon Papi Mar 26 '14 at 19:12
  • 11
    It is possible to access the list of notification listeners, which allows to to check if your app is one of them: `private boolean isNotificationServiceRunning() { ContentResolver contentResolver = getContentResolver(); String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners"); String packageName = getPackageName(); return enabledNotificationListeners != null && enabledNotificationListeners.contains(packageName); }` – Waboodoo Jun 01 '15 at 09:05
  • 1
    if you are like me and would prefer to use the constant, its Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS – hapticdata Aug 14 '15 at 19:38
  • @hapticdata Ah, nice. Looks like that was added in API 22. – adneal Aug 14 '15 at 19:41
  • I m always getting false value in nexus 5, API 23, even after giving permissions. – Panache Jun 23 '17 at 15:04
  • @Panache to CHECK for permissions, you should use: `NotificationManagerCompat.from(this).areNotificationsEnabled()` which works after API-19. Before it will always return true. – inteist Oct 29 '17 at 16:16
  • How to open this settings page only for a specific app, such that only one specific app is in the list to enable it. We can achieve similar thing for `android.settings.action.MANAGE_OVERLAY_PERMISSION` by setting app's package name like so: `new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS, "app's package name")` – VahidShir Sep 19 '19 at 10:44
5

Many Thanks to @adneal and @Waboodoo. I am posting this for an complete answer

Check permission granted or not using this method

private boolean isNotificationServiceRunning() {
    ContentResolver contentResolver = getContentResolver();
    String enabledNotificationListeners =
            Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
    String packageName = getPackageName();
    return enabledNotificationListeners != null && enabledNotificationListeners.contains(packageName);
}

Then show settings activity, if necessary

boolean isNotificationServiceRunning = isNotificationServiceRunning();
if(!isNotificationServiceRunning){
    startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
}
mili
  • 3,502
  • 1
  • 29
  • 29
  • 2
    For a simpler way to check if access is granted you can also use: `NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.packageName)` (see [henrichg's answer here](https://stackoverflow.com/a/40185207/4464562)) – thando Oct 21 '19 at 10:12
1

You can always use the notification manager to check if notifications are enabled at the OS level:

NotificationManagerCompat.from(context).areNotificationsEnabled()