5

Android 8.0 and above brought notification channels. Is there any way to list all notification channels, create channels and/or disable channels for an app from ADB? Root solutions are acceptable too. Thank you.

SayantanRC
  • 799
  • 7
  • 23

2 Answers2

0

You can create a notification channel, delete a channel if you want using service call superuser command.

Here is a small example by which you can enable or disable notifications for any app. By using that reference you can get notification channels information.

public final Shell.Interactive su = new Shell.Builder().useSU().open();
function setNotification(String packageName, int uid, boolean enable){
    try {
          @SuppressLint("PrivateApi") Field field = Class.forName("android.app.INotificationManager").getDeclaredClasses()[0].getDeclaredField("TRANSACTION_setNotificationsEnabledForPackage");
          field.setAccessible(true);
          int id = field.getInt(null);
          su.addCommand(String.format(Locale.ENGLISH, "service call notification %d s16 %s i32 %d i32 %d", id, packageName, uid, enable ? 1 : 0));
   } catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
          e.printStackTrace();
   }
}

here service call is used to call the underlying service, id is used to identify the function and s16 is for String input and i32 is for int input they are just parameters of the function we are calling in notification service.

You can find explanation to service call from here:

Where to find info on Android's "service call" shell command?

You can find reference to INotificationManager from here:

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/INotificationManager.aidl

As you can see there is a function called setNotificationsEnabledForPackage in the INotificationManager which I have used in the example. There are many functions related to notification channel like getNotificationChannel just use which suits you and replace it with setNotificationsEnabledForPackage and pass appropriate parameters.

Here su is from this library:

https://github.com/Chainfire/libsuperuser

RHS.Dev
  • 412
  • 6
  • 18
  • What's the "uid" parameter? Where do you get it from? – android developer Jul 20 '22 at 07:38
  • uid parameter is user ID and if your device does not have multiple user it will be 0 by default. You can get list of uids using "pm" command. – RHS.Dev Jul 20 '22 at 14:23
  • I thought that maybe it's `Process.myUid()` . Isn't there an official API to get the "uid" you are talking about (of the current user)? Can you please share the code to get it, based on what you've found? Also isn't there a better way than this reflection? Maybe using adb? – android developer Jul 20 '22 at 20:07
-1

You could try

adb shell dumpsys notification

This will show the detail about NotificationManagerService.

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
cowboi-peng
  • 777
  • 2
  • 6
  • 24