How to get Override Do not Disturb Settings Value?

本小妞迷上赌 提交于 2019-12-21 03:46:07

问题


How do I get Override Do Not Disturb Settings state in Android for my own package app?


回答1:


The documentation is not very clear how to show this option, just that it's possible:

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.

But according to my testes, on Android 8.0+ you have this option only for notification channels that have set the importance to Urgent, that corresponds to NotificationManager.IMPORTANCE_HIGH. For more info about creating a channel, see Create a channel and set the importance.

On Android 5.0 to 7.1, it's said you have to use 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.

So I tried with NotificationCompat.PRIORITY_MAX, but I didn't manage to see the Override Do Not Disturb option until I also added a system-wide category, something like:

 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);

Now, for Android 8.0+, to see what settings an user has applied to your channel, Read notification channel settings suggests using 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();
    }

Unfortunately, under 7.1 there doesn't seem to be any public method to get that info; the only one available for NotificationManagerCompat is areNotificationsEnabled().



来源:https://stackoverflow.com/questions/50724078/how-to-get-override-do-not-disturb-settings-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!