如何定义NotificationChannel是否被用户禁用(API 26)?

时间:2017-09-14 11:11:38

标签: android android-notifications android-support-library

我将我的代码更新为API v26,设置了NotificationChannels,我可以看到我的通知,但我有关于禁用通知的逻辑。 在26岁之前我有类似的事情:

NotificationManagerCompat.from(context).areNotificationsEnabled()

现在这似乎没用。那么如何知道设置中是否禁用了通知通道呢?

1 个答案:

答案 0 :(得分:9)

我发现新的 ChannelNotification 方法并不能替代旧逻辑,它会为通知添加一层控制。 所以现在我们有2个场景,请参见截图:

enter image description here

  1. 您可以定义是否启用通知:

      NotificationManagerCompat.from(context).areNotificationsEnabled();
    
  2. 您可以定义用户是否可以看到您的通知:

      NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE);       
      NotificationChannel notificationChannel = 
      notificationManager.getNotificationChannel(channelId);      
      int importance = notificationChannel.getImportance();
    
  3. 如果重要性为NotificationManager.IMPORTANCE_NONE,用户将看不到通知,但通知就在那里,因此您可以将其与Foreground服务一起使用,您应该将其解雇。如果重要性为NotificationManager.IMPORTANCE_MIN或更高,则用户会看到通知。