将声音添加到 Xamarin.Android 中的通知



我按照以下链接创建了一个通知:https://learn.microsoft.com/it-it/xamarin/android/app-fundamentals/notifications/local-notifications

//Create notification channel:  
` CreateNotificationChannel():
instance.CreateNotificationChannel("enter","On_Enter");
//Build notification
var builder = new NotificationCompat.Builder(instance,"enter")
.SetSmallIcon(global::Android.Resource.Drawable.IcDialogInfo)
.SetContentTitle("Proximity") // Set the title
.SetContentText($"benvenuto nel beacon di colore {deskOwner}, {description}")
.SetDefaults(NotificationDefaults.Sound)
.Build();
// Get the notification manager:
NotificationManager notificationManager = instance.GetSystemService(Context.NotificationService) as NotificationManager;
// Pubblico la notifica:
const int notificationId = 0;
notificationManager.Notify(notificationId, builder);
return null;
}

当我编写 SetDefaults 将声音添加到通知时,会给我一个错误: 这是错误: 无法从"Android.App.NotificationDefaults"转换为"int"。 我该如何解决它?

您可以使用以下行进行转换。

var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
.SetContentIntent(resultPendingIntent) // Start up this activity when the user clicks the intent.
.SetContentTitle("Button Clicked") // Set the title
.SetNumber(count) // Display the count in the Content Info
.SetDefaults((int)NotificationDefaults.Sound)
.SetSmallIcon(Resource.Drawable.ic_stat_button_click) // This is the icon to display
.SetContentText($"The button has been clicked {count} times."); // the message to display.

但是您不需要设置通知SetDefaults,此方法在 API 级别 2NotificationChannel#setSound(Uri, AudioAttributes)NotificationChannel#enableLights(boolean)NotificationChannel#enableVibration(boolean)6 中已弃用

。你可以看到这个线程 https://developer.android.com/reference/android/app/Notification.Builder#setDefaults(整数(

最新更新