替换过时的Notification.Builder.SetPriority()?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换过时的Notification.Builder.SetPriority()?相关的知识,希望对你有一定的参考价值。

我正在尝试在android上设置通知,并且我得到了Visual Studio的警告,Notification.Builder.SetPriority()已经过时了。

它有替代品吗? Android建议使用SetImportance,但这似乎并未包含在Xamarin中。

答案

此方法在API级别26中已弃用。请改用setImportance(int)。

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

channel.Importance = NotificationImportance.High

要么

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

Conditional API Notification Example:

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();
}

以上是关于替换过时的Notification.Builder.SetPriority()?的主要内容,如果未能解决你的问题,请参考以下文章

java #android #oreo #notification builder

Android学习笔记(32):通知推送Notification

statefuljob 过时 用啥替换

警告:API 'variant.getJavaCompile()' 已过时,已替换为 'variant.getJavaCompileProvider()'

替换过时的 System.Xml.XmlDataDocument?

仍然收到警告:配置“编译”已过时并已替换为“实现”