Check if user has granted NotificationListener access to my app
The only correct way to do it is to check the secure settings:
ComponentName cn = new ComponentName(context, YourNotificationListenerService.class);
String flat = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
final boolean enabled = flat != null && flat.contains(cn.flattenToString());
I was wrong, checking if service is running works:
private boolean isNLServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (NLService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
If you're using androidx you can use
NotificationManagerCompat.getEnabledListenerPackages(Context context)
to check if your listener is enabled. Source