android - 替代过时的 Notification.Builder.SetPriority()?

标签 android xamarin xamarin.android

我正在尝试在 Android 上设置通知,但我收到 Visual Studio 发出的警告 Notification.Builder.SetPriority()已经过时了。

有替代品吗?安卓提示SetImportance但这似乎没有包含在 Xamarin 中。

最佳答案

This method was deprecated in API level 26. use setImportance(int) instead.

SetImportance 包含在 API26 中的 NotificationChannel 中,可以设置为 channel 上的属性,或在 NotificationChannel 构造函数中:

channel.Importance = NotificationImportance.High

channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);

条件 API 通知示例:

var title = "Note from Sushi";
var message = "StackOverflow is a really great source of information";
using (var notificationManager = NotificationManager.FromContext(BaseContext))
{
    Notification notification;
    if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
    {
        notification = new Notification.Builder(BaseContext)
                                             .SetContentTitle(title)
                                             .SetContentText(message)
                                             .SetAutoCancel(true)
                                             .SetPriority(1)
                                             .SetSmallIcon(Resource.Drawable.icon)
                                             .SetDefaults(NotificationDefaults.All)
                                             .Build();
    }
    else
    {
        var myUrgentChannel = PackageName;
        const string channelName = "SushiHangover Urgent";

        NotificationChannel channel;
        channel = notificationManager.GetNotificationChannel(myUrgentChannel);
        if (channel == null)
        {
            channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
            channel.EnableVibration(true);
            channel.EnableLights(true);
            channel.LockscreenVisibility = NotificationVisibility.Public;
            notificationManager.CreateNotificationChannel(channel);
        }
        channel?.Dispose();

        notification = new Notification.Builder(BaseContext)
                                             .SetChannelId(myUrgentChannel)
                                             .SetContentTitle(title)
                                             .SetContentText(message)
                                             .SetAutoCancel(true)
                                             .SetSmallIcon(Resource.Drawable.icon)
                                             .Build();
    }
    notificationManager.Notify(1331, notification);
    notification.Dispose();
}

关于android - 替代过时的 Notification.Builder.SetPriority()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49140479/

相关文章:

android - edittext android中的字符映射

android - 如何指定用于 Android 可穿戴设备的布局?

android - 如何在事件发生后弹出选项菜单?

xaml - Xamarin TapGestureRecognizer 有时无法正常工作

c# - monodroid - 默认新的未修改模板项目无法在 android 模拟器中运行

java - LibGDX:Android 设备上加载纹理缓慢(不断调用垃圾收集器)

xamarin - 使用 Application.Current.Properties 的持久存储不起作用

ios - 如何知道 xamarin iOS 中当前的 Storyboard名称?

xamarin - 解析值 : s. 路径 '',第 0 行,位置 0 时遇到意外字符。FLURL

c# - 如何将 CanExecute 与 Mvvmcross 一起使用