Android Oreo notifications - check if a specific channel is enabled

Im using this snippet to check if notifications are enabled:

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

however , if the user only disconnects the channel, I cannot know about it.

Any idea? enter image description here

+11
source share
6 answers

backward compatible:

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

Check out the docs here .

Users can change notification channel settings, including behaviors such as vibration and alert sound. You can call the following two methods to find out the settings that the user has applied to the notification channel:

To get one notification channel, you can call getNotificationChannel() . To get all the notification channels that belong to your application, you can call getNotificationChannels() . Once you have NotificationChannel , you can use methods such as getVibrationPattern() and getSound() to find out what settings the user currently has. To find out if the user has blocked the notification channel, you can call getImportance() . If the notification channel is blocked, getImportance() returns IMPORTANCE_NONE .

+9
source

This method may 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); } 
+2
source

I think that the example by @itzhar has one drawback: when the user completely turned off notifications in the application, we can get a false positive result (when the channel itself was not disabled).

The updated code (in Kotlin) may look like this:

 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 
+2
source

Use this to check if notifications in general or channels are disabled, and bring the user to the appropriate 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); } 
+2
source
 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(); } 
0
source

Source: https://habr.com/ru/post/1272874/


All Articles