Локальные уведомления появляются, когда мое телефонное приложение Xamarin получает новое событие. Это работает нормально, и это также играет звук. Однако я хочу воспроизводить разные звуки в зависимости от типа события, полученного моим приложением. Я подумал, что это будет достаточно просто, но какой бы звук не воспроизводился первым после перестройки приложения, это звук, который затем воспроизводится для каждого уведомления независимо от кода.
Например, после перекомпоновки, если перечисление Priority enum будет Priority.Temp, тогда будет воспроизводиться TempNotif.wav. Но если код вызывает этот метод снова, чтобы показать другое уведомление, и перечисление Priority не является Priority.Temp, TempNotif.wav по-прежнему воспроизводится, даже если я установил его для воспроизведения StandardNotif.wav. Похоже, что android берет копию первого звука и не потрудится обновить его, как только появится звук. Что бы ни происходило, конечно, я могу воспроизводить разные звуки для разных уведомлений?
Вот код:
public void CreateNotification(string title, string message, Priority priority)
{
Android.Net.Uri sound = null;
if (priority == Priority.Temperature)
{
sound = global::Android.Net.Uri.Parse($!{ContentResolver.SchemeAndroidResource}://{context.PackageName}/{Resource.Raw.TempNotif}");
}
else
{
sound = global::Android.Net.Uri.Parse($!{ContentResolver.SchemeAndroidResource}://{context.PackageName}/{Resource.Raw.StandardNotif}");
}
var alarmAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
builder = new NotificationCompat.Builder(context, notificationChannelId);
builder.SetContentTitle(title)
.SetAutoCancel(true)
.SetContentText(message)
.SetPriority((int)NotificationPriority.High)
.SetSmallIcon(Resource.Drawable.MetroIcon)
.SetColor(00255)
.SetVibrate(new long[0])
.SetSound(sound)
.SetVisibility((int)NotificationVisibility.Public);
notificationManager = context.GetSystemService(Context.NotificationService) as
NotificationManager;
if (global::Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
NotificationImportance importance = NotificationImportance.High;
NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId,
title, importance);
notificationChannel.Importance = NotificationImportance.High;
notificationChannel.EnableLights(true);
notificationChannel.EnableVibration(true);
notificationChannel.SetSound(sound, alarmAttributes);
notificationChannel.SetShowBadge(true);
notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });
if (notificationManager != null)
{
builder.SetChannelId(notificationChannelId);
notificationManager.CreateNotificationChannel(notificationChannel);
}
}
notificationManager.Notify(0, builder.Build());
}