【问题标题】:Add sound to Notification in Xamarin.Android在 Xamarin.Android 中向通知添加声音
【发布时间】:2019-10-02 13:32:13
【问题描述】:

我在此链接后创建了一个通知:https://docs.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”。 我该如何解决?

【问题讨论】:

  • 您可以尝试将 NotificationDefaults.Sound 转换为 int 或使用 Notification.Builder(已弃用)。

标签: c# android xamarin xamarin.android notifications


【解决方案1】:

你可以使用下面的行来转换它。

   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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多