我创建了一个dependencyservice,但是在android中,自定义通知声音无法播放。
尝试的方法:
我的音频文件位置是资源/原始文件夹
有人遇到过这个问题吗?
这是我的代码:
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;
}
}
答案 0 :(得分:1)
您创建的URI错误,因此未选择它。它的外观应类似于:android.resource://<packagename>/<resourceId>
所以在您的情况下,这样的方法应该起作用:
var uri = Uri.Parse($"android.resource://{AndroidApp.Context.PackageName}/{Resources.Raw.alarm}");
鉴于文件警报位于Resources/raw/