如何获得覆盖请勿打扰设置值?

How to get Override Do not Disturb Settings Value?

如何在 Android 中为我自己的应用程序包获取“覆盖请勿打扰”设置状态?

documentation不是很清楚如何显示这个选项,只是说这是可能的:

On Android 8.0 (API level 26) and above, users can additionally allow notifications through for app-specific categories (also known as channels) by overriding Do Not Disturb on a channel-by-channel basis. [...] On devices running Android 7.1 (API level 25) and below, users can allow notifications through on an app by app basis, rather than on a channel by channel basis.

但是根据我的睾丸,在 Android 8.0+ 上你只有设置了 importance to Urgent, that corresponds to NotificationManager.IMPORTANCE_HIGH. For more info about creating a channel, see Create a channel and set the importance.

的通知渠道才有这个选项

在Android 5.0到7.1上,据说你必须使用setPriority()

On Android 8.0 (API level 26) and above, importance of a notification is determined by the importance of the channel the notification was posted to. Users can change the importance of a notification channel in the system settings (figure 12). On Android 7.1 (API level 25) and below, importance of each notification is determined by the notification's priority.

所以我尝试了 NotificationCompat.PRIORITY_MAX, but I didn't manage to see the Override Do Not Disturb option until I also added a system-wide category, 类似于:

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("Notification title")
                    .setContentText("Text content")
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setCategory(NotificationCompat.CATEGORY_ALARM);

现在,对于 Android 8.0+,要查看用户对您的频道应用了哪些设置,Read notification channel settings 建议使用 canBypassDnd() from getNotificationChannel():

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = manager.getNotificationChannel(CHANNEL_ID);
        channel.canBypassDnd();
    }

不幸的是,在 7.1 下似乎没有任何 public 方法来获取该信息; NotificationManagerCompat 唯一可用的是 areNotificationsEnabled().