Android 如何在本地通知上播放不同的声音文件
当我的Xamarin手机应用程序收到新事件时,会弹出本地通知。这很好用,而且还能播放声音。但是,我想根据我的应用程序接收到的事件类型播放不同的声音。我原以为这很简单,但在我重新构建应用程序后,无论首先播放什么声音,都是在每次通知中播放的声音,无论代码如何 例如,重建后,如果Priority enum为Priority.Temp,则将播放TempNotif.wav。但是,如果代码再次调用此方法以显示另一个通知,并且Priority enum不是Priority.Temp,则TempNotif.wav仍会播放,即使我已将其设置为play StandardNotif.wav。安卓系统似乎复制了第一个声音,一旦有声音出现,就不需要更新了。不管发生什么事,我当然可以为不同的通知播放不同的声音 以下是代码:Android 如何在本地通知上播放不同的声音文件,android,visual-studio,xamarin,notifications,Android,Visual Studio,Xamarin,Notifications,当我的Xamarin手机应用程序收到新事件时,会弹出本地通知。这很好用,而且还能播放声音。但是,我想根据我的应用程序接收到的事件类型播放不同的声音。我原以为这很简单,但在我重新构建应用程序后,无论首先播放什么声音,都是在每次通知中播放的声音,无论代码如何 例如,重建后,如果Priority enum为Priority.Temp,则将播放TempNotif.wav。但是,如果代码再次调用此方法以显示另一个通知,并且Priority enum不是Priority.Temp,则TempNotif.wa
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());
}
感谢@moonian的回答 答案是为我要显示的每种类型的通知提供不同的通道ID
private static string StandardchannelId = "1000";
private static string TempChannelId = "2000";
我为通道ID设置了两个静态值,并根据要显示的通知类型提供一个静态值
private static string StandardchannelId = "1000";
private static string TempChannelId = "2000";
在这里,我根据我创建的枚举分配我的channelID:
if (priority == Priority.Temperature)
{
this.channelId = TempChannelId;
sound = global::Android.Net.Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{context.PackageName}/{Resource.Raw.TempNotif}");
}
else
{
this.channelId = StandardchannelId;
sound = global::Android.Net.Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{context.PackageName}/{Resource.Raw.StandardNotif}");
}
在这里,我分配channelID:
builder.SetChannelId(this.channelId);
您需要覆盖您的通知吗?如果不是,则在调用notificationManager.Notify(0,builder.Build())时尝试传递唯一id,而不是始终传递0;我回到电脑前会查一下的谢谢bro@DavidAndrewThorpeAPI级别26中不推荐使用Notification.Builder setSound方法。改为使用NotificationChannel#setSound(Uri,AudioAttributes)。请看一看:@CherryBu MSFT我使用的是NotificationCompat.Builder,我认为它没有贬值吗?@CherryBu MSFT如果你看我代码的下半部分,你会发现我使用的是NotificationChannel.SetSound(),听说你现在已经解决了你的问题,请记住将你的回答标记为答案,这可能对其他社区成员有所帮助,谢谢。