Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know programmatically if a notification channel is enabled on Android O?

Tags:

android

Looking at the docs I can see methods for checking all the attributes of a notification channel, but I couldn't find a way to check if the channel itself is enabled or disabled.

Am I missing something?

like image 904
jmart Avatar asked Oct 10 '17 15:10

jmart


People also ask

What are notification channels on Android?

“Notification Channels” were introduced in 2017 with Android 8.0 Oreo. The idea is that apps can group different types of notifications into “channels.” Each channel can then be turned on or off by the user. This all happens in the Android settings. Let’s take a look at YouTube, for example. There are quite a few channels available.

Why do I need a notification channel group for my App?

This is useful for apps that support multiple user accounts. The same notification channels are available across the individual accounts. For example, a social networking app might include support for both personal and business user accounts. The code below shows you how to create a notification channel group:

How do I change the notification settings for a channel?

In the channel notification settings, users can edit settings such as enabling vibrations, changing the importance, or showing a badge (if supported) for the channel. If you want to take users to the general notification settings for your app, you can also do that with an Intent:

Why do I need a channel ID to set a notification?

As of Android O, creating a Notification instance now requires a channel ID to be set by the use of the setChannel () method. This is to ensure that our notification belongs to a channel that can be managed by the user from their device settings.


1 Answers

Here is my full code:

public static boolean isNotificationChannelEnabled(@NonNull String groupId, @NonNull String... channelIds) {
    boolean appNotificationEnable = NotificationManagerCompat.from(AppContext.getContext()).areNotificationsEnabled();
    if (!appNotificationEnable) {
        return false;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager manager = (NotificationManager) AppContext.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            List<NotificationChannelGroup> groupList = manager.getNotificationChannelGroups();
            for (NotificationChannelGroup group : groupList) {
                if (TextUtils.equals(group.getId(), groupId)) {
                    if (group.isBlocked()) {
                        return false;
                    }
                }
            }
        }

        for (String channelId : channelIds) {
            NotificationChannel channel = manager.getNotificationChannel(channelId);
            if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                return false;
            }
        }
        return true;
    }

    return false;
}
like image 107
thirtyyuan Avatar answered Oct 21 '22 23:10

thirtyyuan