如何确定用户是否禁用了通知渠道(API 26)?

6

我已将我的代码更新到API v26,设置了NotificationChannels,并且可以看到我的通知,但是我有关于禁用通知的逻辑。

在API v26之前,我有类似以下的逻辑:

NotificationManagerCompat.from(context).areNotificationsEnabled()

现在看来这似乎没有用处。那么,怎样才能知道通知渠道在设置中被禁用了呢?


1
也许通知渠道的重要性会变成IMPORTANCE_NONE?我不确定,只是猜测。 - azizbekian
1
@azizbekian 谢谢你的假设,事实证明你是正确的。 - Viktor Yakunin
1个回答

10
我发现新的ChannelNotification方法不是取代旧逻辑,而是为通知添加了一层控制。现在我们有两种场景,见截图:

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();
    
如果优先级是NotificationManager.IMPORTANCE_NONE,用户不会看到通知,但通知确实存在,因此您可以将其与前台服务一起使用,并应该将其关闭。 如果优先级为NotificationManager.IMPORTANCE_MIN或更高,则用户将看到通知。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接