androidandroid-notificationsandroid-8.0-oreo

How do I check whether a specific notification channel is enabled in Android Oreo?


I'm using the following call to check whether notifications are enabled:

NotificationManagerCompat.from(getContext()).areNotificationsEnabled()

However, if a user disables only the channel, I cannot know about it.

How do I check whether a specific notification channel is enabled?

Screenshot of Notifications screen in Settings


Solution

  • with backwards compatibility:

    public boolean isNotificationChannelEnabled(Context context, @Nullable String channelId){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if(!TextUtils.isEmpty(channelId)) {
                    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    NotificationChannel channel = manager.getNotificationChannel(channelId);
                    return channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
                }
                return false;
            } else {
                return NotificationManagerCompat.from(context).areNotificationsEnabled();
            }
        }