How to determine if NotificationChannel has been disabled by the user (API 26)?

I updated my code to API v26, configured NotificationChannels, and I can see my notifications, but I have logic regarding disconnect notifications. Until 26, I have something like:

NotificationManagerCompat.from(context).areNotificationsEnabled() 

And that doesn't seem to be useful right now. So, how do you know if the notification channel is disabled in the settings?

+6
source share
1 answer

I found that the new ChannelNotification approach does not replace the old logic; it adds another level of control for notifications. So, now we have 2 scenarios, see screenshot:

enter image description here

  • You can determine if notifications are enabled:

      NotificationManagerCompat.from(context).areNotificationsEnabled(); 
  • You can determine if your notification is visible to the user:

      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId); int importance = notificationChannel.getImportance(); 

If the value is NotificationManager.IMPORTANCE_NONE , the user will not see the notification, but there is a notification, so you can use it using the Foreground service, and you must reject it. If the value of NotificationManager.IMPORTANCE_MIN or higher, the user will see a notification.

+9
source

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


All Articles