Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 979
Viktor Yakunin Avatar asked Sep 14 '17 11:09

Viktor Yakunin


1 Answers

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.

like image 166
Viktor Yakunin Avatar answered Sep 28 '22 02:09

Viktor Yakunin