我正在开发一个应用程序,该应用程序需要覆盖DND并显示带有警报声的通知。您能否建议是否可行?
我已经使用NotificationChannel使用canBypassDnd()覆盖了DND,但是即使我启用了setBypassDnd(true),它也会返回false。
我还添加了通知设置屏幕,是否可以切换DND模式?
下面是我的通知和DND代码。
免打扰代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALARMS);
}
通知代码
private int notificationIndex = 0;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Dream",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
**notificationChannel.setBypassDnd(true);**
mNotificationManager.createNotificationChannel(notificationChannel);
}
**if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
channel.canBypassDnd();
}**
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
.setContentTitle(getString(R.string.app_name))
.setContentText("Test Notification")
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true);
mNotificationManager.notify(notificationIndex, notificationBuilder.build());
notificationIndex++;
即使启用了DND,我也要用声音显示通知。