c# - 在 Xamarin.Android 中向通知添加声音

标签 c# android xamarin xamarin.android notifications

我通过此链接创建了一个通知: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 级别 26 中已弃用。使用 NotificationChannel#enableVibration(boolean)NotificationChannel# enableLights(boolean)NotificationChannel#setSound(Uri, AudioAttributes) 代替。

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

关于c# - 在 Xamarin.Android 中向通知添加声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58202622/

相关文章:

java - 理解 "Sprite' s onDraw()"方法时遇到问题

android - Google Maps API - 从郊区的起点到终点标记一条街道

android - 如何更改 Android SDK 管理器的安装位置?

c# - 为什么 ILogger 不记录到 Azure 应用程序见解?

c# - Fluent Validation 验证器在添加验证代码之前就会导致错误

c# - 是否应该在 Silverlight 中重用 WebClient 实例

java - 拥有大量的 childEventListener 是一个好习惯吗?

重定向标准错误的 C# 问题

使用相机 Intent 时,Android 局部变量丢失

c# - 如何重命名 Xamarin 绑定(bind)库中的属性