Android Oreo Notifications - check if specific channel enabled

Check out the docs here.

Users can modify the settings for notification channels, including behaviors such as vibration and alert sound. You can call the following two methods to discover the settings a user has applied to a notification channel:

To retrieve a single notification channel, you can call getNotificationChannel(). To retrieve all notification channels belonging to your app, you can call getNotificationChannels(). After you have the NotificationChannel, you can use methods such as getVibrationPattern() and getSound() to find out what settings the user currently has. To find out if a user blocked a notification channel, you can call getImportance(). If the notification channel is blocked, getImportance() returns IMPORTANCE_NONE.


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();
        }
    }

Use this to check if either the notifications overall or the channels are disabled and bring the user to the corresponding settings:

In the calling method:

if (!notificationManager.areNotificationsEnabled()) {
        openNotificationSettings();
        return;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
            isChannelBlocked(CHANNEL_1_ID)) {
        openChannelSettings(CHANNEL_1_ID);
        return;
    }

In your class:

private void openNotificationSettings() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
        intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
        startActivity(intent);
    } else {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }
}

@RequiresApi(26)
private boolean isChannelBlocked(String channelId) {
    NotificationManager manager = getSystemService(NotificationManager.class);
    NotificationChannel channel = manager.getNotificationChannel(channelId);

    return channel != null &&
            channel.getImportance() == NotificationManager.IMPORTANCE_NONE;
}

@RequiresApi(26)
private void openChannelSettings(String channelId) {
    Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
    intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
    intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
    startActivity(intent);
}