首页 > 解决方案 > Android:如何绕过请勿打扰以便在后台播放通知音频

问题描述

我的目标是 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);

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

所有通话,

所有消息,

警报/任务开启,

提醒开启

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

有什么我想念的想法吗?

谢谢

标签: androidfirebasepush-notificationdo-not-disturb

解决方案


推荐阅读