3

I have a working implementation, to check if a channel is enabled.

But is there any possible way to disable a notification channel programmatically?

How I check if the channel is enabled:

    /**
     * Get the setting a user has applied to the notification channel.
     * If the android API level is < 26, it will return true if all notification
     * are enabled in general, false otherwise.
     *
     * @return true if the channel is enabled, false otherwise
     */
    public static boolean isChannelEnabled(String channelId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final NotificationManager notificationManager = App.get().getSystemService(NotificationManager.class);
            if (notificationManager == null) {
                return true;
            } else {
                final NotificationChannel c = notificationManager.getNotificationChannel(channelId);
                final boolean overallEnabled = notificationManager.areNotificationsEnabled();
                return overallEnabled && c != null && NotificationManager.IMPORTANCE_NONE != c.getImportance();
            }
        } else {
            return NotificationManagerCompat.from(App.get()).areNotificationsEnabled();
        }
    }


ceng
  • 128
  • 3
  • 17
JacksOnF1re
  • 3,336
  • 24
  • 55
  • 4
    Try channel.getImportance() = IMPORTANCE_NONE; – Kristy Welsh Mar 20 '20 at 14:14
  • @KristyWelsh that's so crazy I had to reply to a 3 year old comment. You're trying to assign a value to a function? – Niall Aug 04 '23 at 14:56
  • @Niall Yes that won't be possible ;) But you can create the channel at app start up and use `IMPORTANCE_NONE` in the NotificationChannel ctor. That does actually work and disable the channel. – JacksOnF1re Aug 07 '23 at 14:29

1 Answers1

0

As Kristy Welsh mentioned, it is possible to disable a channel by setting the importance of the channel to NotificationManager.IMPORTANCE_NONE.

As the documentation of NotificationManager.createNotificationChannel states:

This can also be used to restore a deleted channel and to update an existing channel's name, description, group, and/or importance. (...) The importance of an existing channel will only be changed if the new importance is lower than the current value and the user has not altered any settings on this channel.

Note: So it is indeed possible to disable a channel. But it won't be possible to set it back to some higher importance.

Some pseudo code:

val channel = NotificationChannel(someId, name, NotificationManager.IMPORTANCE_NONE).apply { /* ... */ }
val notificationManager = application.getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannelGroup(channel)

I need to say, that I do not need this "feature" anymore, but I have tested it anyway on Pixel 7 and Android 13. For me it was possible to degrade the importance to disable the channel. It's not possible to enable the channel again afterwards programmatically. (See docu above).

JacksOnF1re
  • 3,336
  • 24
  • 55