notification-listener-service-example icon indicating copy to clipboard operation
notification-listener-service-example copied to clipboard

About "isNotificationServiceEnabled"...

Open AndroidDeveloperLB opened this issue 5 years ago • 8 comments

It's much easier to use this code instead:

fun isNotificationServiceEnabled(context: Context): Boolean =NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.packageName)

Much better than this:

https://github.com/Chagall/notification-listener-service-example/blob/master/app/src/main/java/com/github/chagall/notificationlistenerexample/MainActivity.java

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

AndroidDeveloperLB avatar Aug 09 '18 07:08 AndroidDeveloperLB

When this code was implemented, Kotlin was not supported by Android Studio. I did not test the snippet you send, but I don't doubt that it really works. I am just not going to change the method in the codebase to avoid mixing Kotlin and java in the same project, so people don't get confused with mixed languages.

Chagall avatar Jun 04 '19 14:06 Chagall

You can easily convert it to Java. Here:

public static boolean isNotificationServiceEnabled(Context context ){
    return NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.getPackageName());
}

AndroidDeveloperLB avatar Jun 04 '19 14:06 AndroidDeveloperLB

If you could make the changes and send a pull request I will be glad to test and accept it if everything runs fine.

Chagall avatar Jun 05 '19 04:06 Chagall

It's an official API , which was available for a long time: https://developer.android.com/reference/android/support/v4/app/NotificationManagerCompat#getenabledlistenerpackages

AndroidDeveloperLB avatar Jun 05 '19 15:06 AndroidDeveloperLB

OK here: https://github.com/Chagall/notification-listener-service-example/pull/7

I also updated various other stuff, to match what we use today, and handled some warnings that the IDE and Lint showed. I could even convert to Kotlin, but I didn't want to :)

AndroidDeveloperLB avatar Jun 05 '19 16:06 AndroidDeveloperLB

What do you mean " without user interaction" ? And why do you ask this here? It has nothing to do with the topic . Please ask in a normal, new thread.

AndroidDeveloperLB avatar Jul 01 '19 12:07 AndroidDeveloperLB

@Chagall Did you check it out?

AndroidDeveloperLB avatar Aug 14 '19 07:08 AndroidDeveloperLB

Went afk from this account for a long time, will check this as soon as I can. Better late than never.

Chagall avatar Mar 19 '21 00:03 Chagall