Xamarin SetSound-此API现在已过时。使用什么?
SetSound-此API现在已过时。使用什么? 我可以在中使用Xamarin SetSound-此API现在已过时。使用什么?,xamarin,xamarin.android,Xamarin,Xamarin.android,SetSound-此API现在已过时。使用什么? 我可以在中使用.SetDefaults(Resource.Drawable.MYSOUNDMP3)吗 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .SetContentTitle ("Sample Notification") .SetContentText ("Hello Wor
.SetDefaults(Resource.Drawable.MYSOUNDMP3)
吗
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetContentTitle ("Sample Notification")
.SetContentText ("Hello World! This is my first notification!")
.SetDefaults (NotificationDefaults.Sound)
.SetSmallIcon (Resource.Drawable.ic_notification);
我就是这样做的,一切正常
我创建频道
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
string name = "MyName";
var description = "Notice";
var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.High)
{
Description = description
};
var soundUri = Android.Net.Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{Application.Context.PackageName}/{Resource.Drawable.mysound}");
var audioAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification)
.Build();
channel.SetSound(soundUri, audioAttributes);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
将AudioUsageKind.Alarm更改为AudioUsageKind。从API级别26开始,Android端不推荐使用通知。您可以在中阅读更多关于它的信息
var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetContentTitle(SetContentTitle)
.SetContentText(SetContentText)
.SetChannelId(CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.icon);
// Finally, publish the notification:
var notificationManager = NotificationManagerCompat.From(this);
// Publish the notification:
int notificationId = 1;
notificationManager.Notify(notificationId, builder.Build());
如文档所示,您可以使用NotificationChannel的SetSound
。
您可以在Xamarin中找到如何使用NotificationChannel的示例。SetSound在Android上从API级别26被弃用
SetDefaults
应替换为使用以下内容
- NotificationChannel.EnableVibration(布尔值)
- NotificationChannel.EnableLights(布尔值)
- NotificationChannel.SetSound(Uri,AudioAttributes)
setSound
应使用
- NotificationChannel.SetSound(Uri,AudioAttributes)
您可以使用
NotificationChannel
而不是NotificationCompat.Builder
NotificationChannel mChannel;
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
mChannel = new NotificationChannel(CHANNEL_ID, Utils.CHANNEL_NAME, Android.App.NotificationImportance.High);
string description = getString(R.string.channel_description);
mChannel.LightColor(Color.GRAY);
mChannel.EnableLights(true);
mChannel.Description=description ;
var audioAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Alarm)
.Build();
var alarmUri = Android.Net.Uri.Parse("MyApp.Android/Resources/raw/alarm.mp3");
mChannel.SetSound(soundUri, audioAttributes);
if (mNotificationManager != null)
{
mNotificationManager.createNotificationChannel(mChannel);
}
}
有关更多信息,请参阅此文档
有什么答案对你有用吗?@r15我试着使用NotificationChannel,正如这里建议的那样。但是我遇到了一个我在这里描述的问题,所以我很难给你一个答案。我没有实现真正的声音。@r15我计算出我的新问题需要一个新主题。在我的决定中,我依据的规则是:一个新话题需要一个新话题。当我明白是什么解决了我的问题时,我一定会说谢谢。我不能对每个人说谢谢。我当然可以,但当其他用户发现哪个答案有用时。虽然对我个人来说,所有的答案都很有帮助。我真的不想说“谢谢”,但任何解决你的问题或接近问题的答案你都应该给它打分/投票。如果有任何疑问,请询问他们,而不是消失。嗨:)我试着按照你的例子来实现。我转向文档,按照这里写的做了。我在这里发布了我的代码,但是我没有介绍声音,直到我实现了一个简单的通知显示。请帮帮我。你们可以调试你们的代码,检查控制是否进入内部,若条件和否。我正在一个真实的设备上测试。组装过程中没有错误。尽管这是我在文档中忘记提到的,它需要使用TaskStackBuilder=Android.Support.V4.App.TaskStackBuilder编写代码
代码>我的VS告诉我不需要此代码。您设备的Android版本是什么。你能调试它吗?你可以忽略警告