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?
“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.
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:
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:
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With