Skip to content

Instantly share code, notes, and snippets.

@ochim
Created June 19, 2019 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochim/801e96357742d57c8d104ff16076b42d to your computer and use it in GitHub Desktop.
Save ochim/801e96357742d57c8d104ff16076b42d to your computer and use it in GitHub Desktop.
Android 通知チャンネルを有効にしているかどうか判定する

https://stackoverflow.com/questions/46928874/android-oreo-notifications-check-if-specific-channel-enabled

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

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

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment