android - 自定义通知声音不会在 android 中播放
问题描述
我创建了一个依赖服务,但在 android 中自定义通知声音不播放。
什么尝试:
- 我创建了很多版本,但结果都是一样的,没有自定义声音。
- 我可以设置默认声音,它的播放效果很好,但我只需要自定义声音。
- 我的第一个想法是 uri,这可能是错误的,但我创建了 uri 的 4-5 个版本,但结果是一样的。
我的音频文件位置是资源/原始文件夹
以前有人遇到过这个问题吗?
这是我的代码:
public class AndroidNotificationManager : INotificationManager
{
const string channelId = "default";
const string channelName = "Default";
const string channelDescription = "The default channel for notifications.";
const int pendingIntentId = 0;
public const string TitleKey = "title";
public const string MessageKey = "message";
bool channelInitialized = false;
NotificationManager manager;
private string soundFileName;
public event EventHandler NotificationReceived;
public void Initialize()
{
CreateNotificationChannel();
}
public int ScheduleNotification(string title, string message, int messageId, string soundFileName = "")
{
if (!channelInitialized)
{
CreateNotificationChannel();
}
this.soundFileName = soundFileName;
Intent intent = new Intent(AndroidApp.Context, typeof(MainActivity));
intent.PutExtra(TitleKey, title);
intent.PutExtra(MessageKey, message);
PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId, intent, PendingIntentFlags.OneShot);
NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)
.SetContentIntent(pendingIntent)
.SetContentTitle(title)
.SetContentText(message)
.SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.ic_launcher))
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetVibrate(null);
if (!string.IsNullOrEmpty(soundFileName))
{
builder.SetDefaults((int)NotificationDefaults.Lights);
var uri = Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{AndroidApp.Context.PackageName}/raw/alarm.wav");
builder.SetSound(uri);
}
else
{
builder.SetDefaults((int) NotificationDefaults.Lights);
builder.SetSound(null);
}
Notification notification = builder.Build();
manager.Notify(messageId, notification);
return messageId;
}
public void ReceiveNotification(string title, string message)
{
var args = new NotificationEventArgs()
{
Title = title,
Message = message,
};
NotificationReceived?.Invoke(null, args);
}
void CreateNotificationChannel()
{
manager = (NotificationManager)AndroidApp.Context.GetSystemService(AndroidApp.NotificationService);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channelNameJava = new Java.Lang.String(channelName);
var channel = new NotificationChannel(channelId, channelNameJava, NotificationImportance.Default)
{
Description = channelDescription
};
channel.SetVibrationPattern(new long[] { 0 });
channel.EnableVibration(true);
channel.EnableLights(true);
// Creating an Audio Attribute
var alarmAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
if (!string.IsNullOrEmpty(soundFileName))
{
var uri = Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{AndroidApp.Context.PackageName}/raw/alarm.wav");
channel.SetSound(uri, alarmAttributes);
}
else
{
channel.SetSound(null, null);
}
manager.CreateNotificationChannel(channel);
}
channelInitialized = true;
}
}
解决方案
您创建的 URI 是错误的,这就是它没有被拾取的原因。它需要看起来像:android.resource://<packagename>/<resourceId>
所以在你的情况下,这样的事情应该有效:
var uri = Uri.Parse($"android.resource://{AndroidApp.Context.PackageName}/{Resources.Raw.alarm}");
鉴于文件警报在Resources/raw/
推荐阅读
- php - Woocommerce 产品简码(按产品类别)
- python - 使用 sum 函数的不支持的操作数类型
- java - 如何在最新的 Chrome Driver Ver: 2.40 中启用 Headless Chrome 中的下载文件
- c# - MvxObservableCollection 添加范围
- php - 如何将 SQL 3.4 数据库导入 phpMyAdmin?
- r - 确认两行有数据
- nuget - 为什么 MSTest.TestAdapter 将其 DLL 添加到我的 NuGet 包中?
- typescript - Apollo GraphQL 服务器 + TypeScript
- python - 即使 AssertionError 也导出 pytest 结果
- c++ - 如何获取崩溃的 DLL 的堆栈跟踪?