Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Oreo Notifications - check if specific channel enabled

Im using this snippet to check if notifications enabled:

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

however, if user disable only the channel, i cannot know about it.

Any idea? enter image description here

like image 352
itzhar Avatar asked Oct 25 '17 09:10

itzhar


Video Answer


2 Answers

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();         }     } 
like image 109
itzhar Avatar answered Sep 28 '22 06:09

itzhar


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.

like image 38
ditn Avatar answered Sep 28 '22 06:09

ditn