安卓 :如何绕过请勿打扰,以便它在后台播放通知音频



我的目标是 Android 8.0 及更高版本,并希望我的远程通知在以下情况下播放频道配置的音频声音

a( 设备处于请勿打扰模式(启用例外(

b( 设备在后台。

通知在未处于免打扰模式时按预期工作。 在免打扰模式下,前台时有视觉通知,但后台没有音频。

我按如下方式设置了我的频道:

private void InitialiseNotificationChannel(NotificationManager notificationManager, string channelID, string name, string soundFilename)
{
// create channel
NotificationChannel channel = new NotificationChannel(channelID, name, NotificationImportance.High);
// create sound URI
Android.Net.Uri uri = Android.Net.Uri.Parse("android.resource://" + Application.Context.PackageName + "/raw/" + soundFilename);
// create audio attributes
AudioAttributes alarmAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
// setup channel
channel.SetSound(uri, alarmAttributes);
channel.EnableVibration(true);
channel.EnableLights(true);
channel.LightColor = Resource.Color.red;
long[] vibrationPattern = { 100, 200, 300, 400, 500, 600, 1000 };
channel.SetVibrationPattern(vibrationPattern);
// Bypass Do Not Disturb
channel.SetBypassDnd(true);
channel.LockscreenVisibility = NotificationVisibility.Public;
// add channel
notificationManager.CreateNotificationChannel(channel);
}

我还设置了过滤来处理异常:

if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
notificationManager.NotificationPolicy = new NotificationManager.Policy(NotificationPriorityCategory.Events, NotificationPrioritySenders.Any, NotificationPrioritySenders.Any);
notificationManager.SetInterruptionFilter(InterruptionFilter.All);

我在设备上为应用程序选择了所有类型的例外,即:

所有呼叫,

所有消息,

警报/任务打开,

提醒开启

但是在后台收到通知时仍然没有音频声音。

知道我错过了什么吗?

谢谢

请记住,根据文档,SetBypassDnd 只能由系统本身(用户单击设备设置(和通知排名器进行修改。

最新更新