Check for Access to Notifications Using Notificationlistenerservice

Check for access to notifications using NotificationListenerService

Starting with Android 8.1 (SDK 27) you can call isNotificationListenerAccessGranted on the NotificationManager. This is the correct API to use. Older Android versions should use getEnabledListenerPackages as a second best option. Relying on your listener callbacks can give incorrect results. See explanation below.

Im developer of Krome. What have I done to check if service is enabled is add public static variable that changes to true in onBind method and to false in unbind. That is how this service work.

Edit:

public static boolean isNotificationAccessEnabled = false;

@Override
public void onListenerConnected() {
isNotificationAccessEnabled = true;
}

@Override
public void onListenerDisconnected() {
isNotificationAccessEnabled = false;
}

Check if user has granted NotificationListener access to my app

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;
}

Android: How to notification to notification

Have you checked if the user granted your app permission for notification listener

    /**
* Is Notification Service Enabled.
* Verifies if the notification listener service is enabled.
* Got it from: https://github.com/kpbird/NotificationListenerService-Example/blob/master/NLSExample/src/main/java/com/kpbird/nlsexample/NLService.java
* @return True if enabled, false otherwise.
*/
private boolean isNotificationServiceEnabled(){
String pkgName = getPackageName();
final String flat = Settings.Secure.getString(getContentResolver(),
ENABLED_NOTIFICATION_LISTENERS);
if (!TextUtils.isEmpty(flat)) {
final String[] names = flat.split(":");
for (int i = 0; i < names.length; i++) {
final ComponentName cn = ComponentName.unflattenFromString(names[i]);
if (cn != null) {
if (TextUtils.equals(pkgName, cn.getPackageName())) {
return true;
}
}
}
}
return false;
}

if you don't have permission you can open settings app for user to enable permission

    /**
* Build Notification Listener Alert Dialog.
* Builds the alert dialog that pops up if the user has not turned
* the Notification Listener Service on yet.
* @return An alert dialog which leads to the notification enabling screen
*/
private AlertDialog buildNotificationServiceAlertDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(R.string.notification_listener_service);
alertDialogBuilder.setMessage(R.string.notification_listener_service_explanation);
alertDialogBuilder.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
}
});
alertDialogBuilder.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// If you choose to not enable the notification listener
// the app. will not work as expected
}
});
return(alertDialogBuilder.create());
}

Here is complete project for notification listener I found in GitHub


https://github.com/Chagall/notification-listener-service-example

How to start the NotificationListenerService on Android

For listening incoming notifications, their is no need to start it explicitly . First define your service in manifest with the following permission -

android.permission.BIND_NOTIFICATION_LISTENER_SERVICE

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

Another thing to take care is that you must grant the permission either by using the following intent -

Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");

or by granting the notification access manually.



Related Topics



Leave a reply



Submit