gpt4 book ai didi

xamarin - SetSound - 此 API 现在已过时。用什么?

转载 作者:行者123 更新时间:2023-12-01 23:36:29 28 4
gpt4 key购买 nike

SetSound - 此 API 现在已过时。用什么?
我可以用.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);

我就是这样做的,一切正常。

我创建 channel
            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);


我创建通知
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());

将 AudioUsageKind.Alarm 更改为 AudioUsageKind.Notification

最佳答案

SetSound从 API 级别 26 开始,Android 端已弃用。您可以在 official Android API documentation 中阅读有关它的更多信息。

如文档所示,您可以使用 NotificationChannel 的 SetSound反而。
您可以在 Xamarin 中找到如何使用 NotificationChannels 的示例 here

关于xamarin - SetSound - 此 API 现在已过时。用什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57687234/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com