Reputation: 13031
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?
Upvotes: 44
Views: 23351
Reputation: 643
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 != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
return false;
}
}
return true;
}
return false;
}
Upvotes: 3
Reputation: 4463
Just an implementation in Kotlin taking into account areNotificationsEnabled
(For API level >= 26).
@TargetApi(Build.VERSION_CODES.O)
private fun isNotificationChannelEnabled(channelId: String): Boolean {
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
return if (manager.areNotificationsEnabled()){
val channel = manager.getNotificationChannel(channelId)
channel.importance != NotificationManager.IMPORTANCE_NONE
} else false
}
Upvotes: 0
Reputation: 1436
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 callgetNotificationChannels()
. After you have theNotificationChannel
, you can use methods such asgetVibrationPattern()
andgetSound()
to find out what settings the user currently has. To find out if a user blocked a notification channel, you can callgetImportance()
. If the notification channel is blocked,getImportance()
returnsIMPORTANCE_NONE
.
Upvotes: 35
Reputation: 963
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if(NotificationManagerCompat.from(context).areNotificationsEnabled()) {
for (String channelId : channelIds) {
if (!TextUtils.isEmpty(channelId)) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
NotificationChannel channel = manager.getNotificationChannel(channelId);
if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE)
return false;
}
}
}
return true;
}else return false;
}else{
return NotificationManagerCompat.from(context).areNotificationsEnabled();
}
Upvotes: 0
Reputation: 6971
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);
}
Upvotes: 12
Reputation: 13031
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();
}
}
Upvotes: 66
Reputation: 633
I think example by @itzhar have one flaw: when user completely disabled notifications in app, we can get false positive (when channel itself wasn't disabled).
Updated code (in Kotlin) can look like:
fun isNotificationAllowed(context: Context): Boolean {
return if (isOOrLater()) {
areNotificationsEnabled(context) and isChannelDisabled(context)
} else {
areNotificationsEnabled(context)
}
}
private fun areNotificationsEnabled(context: Context) =
NotificationManagerCompat.from(context).areNotificationsEnabled()
@RequiresApi(api = Build.VERSION_CODES.O)
private fun isChannelDisabled(context: Context): Boolean{
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID)
return channel.importance != NotificationManager.IMPORTANCE_NONE
}
private fun isOOrLater() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
Upvotes: 5
Reputation: 309
This method can help :
public boolean isNotificationChannelDisabled(@NonNull String channelId) {
if(!channelId.equals(EMPTY)) {
NotificationChannel channel = getManager().getNotificationChannel(channelId);
return channel.getImportance() == NotificationManager.IMPORTANCE_NONE;
}
return false;
}
private NotificationManager getManager(@NonNull Context context) {
return mManager(android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
Upvotes: 3