Viktor Yakunin
Viktor Yakunin

Reputation: 3266

How to define if NotificationChannel was disabled by user (API 26)?

I updated my code to API v26, set up NotificationChannels and I can see my notifications, but I have logic regarding disabled notifications. Before 26 I have something like:

NotificationManagerCompat.from(context).areNotificationsEnabled()

And this seems to be not useful now. So how can one know if notification channel is disabled in the settings?

Upvotes: 6

Views: 1649

Answers (1)

Viktor Yakunin
Viktor Yakunin

Reputation: 3266

I found that new ChannelNotification approach doesn't replace old logic, it adds one more layer of control for notifications. So now we have 2 scenarios, see Screenshot:

enter image description here

  1. You can define if notifications enabled:

      NotificationManagerCompat.from(context).areNotificationsEnabled();
    
  2. You can define if your notification is visible to user:

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

If importance is NotificationManager.IMPORTANCE_NONE user will not see notification, but the notification is there, so you can use it with Foreground service and you should dismiss it. If importance is NotificationManager.IMPORTANCE_MIN or higher user will see notification.

Upvotes: 10

Related Questions