【问题标题】:Replacement for the obsolete Notification.Builder.SetPriority()?替换过时的 Notification.Builder.SetPriority()?
【发布时间】:2018-03-07 06:23:46
【问题描述】:

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

有替代品吗? Android 建议 SetImportance,但这似乎不包含在 Xamarin 中。

【问题讨论】:

  • setPriority 已过时或已弃用,我怀疑??你可以试试notificationcompat.builder.setPriority

标签: android xamarin xamarin.android


【解决方案1】:

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

SetImportance 包含在 API26 中的 NotificationChannel 中,可以设置为通道上的属性,或在 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();
}

【讨论】:

  • 我假设 Build.VERSION.SdkInt 指的是我正在 构建 的版本,但即使在低于 Oreo 的设备上也能正常运行。对吗?
  • @ispiro Build.VERSION 是在设备上运行的实际操作系统的构建,所以在这种情况下,如果设备比 Oreo 小,请使用旧样式的通知,否则使用新的基于 Ore0 通道的方式。有意义吗?
  • 谢谢。所以我想我们必须使用过时的方法。 (今天很少有设备会运行 O。根据Android,大约 1%。)
  • @ispiro 如果您不选择同时包含旧方法和新方法,并且您的代码在这 1.1% 的 Oreo 设备上运行,可能需要检查您是否遇到通知的静默失败; -(stackoverflow.com/q/47750935/4984832
  • 谢谢!我现在更欣赏微软了。他们在不再支持之前很久就将方法标记为已弃用。如果 Xamarin 可以通过调用适当的平台方法来支持一些过时的方法来为 Android 做同样的事情,那就太好了......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 2021-08-05
  • 2019-09-10
相关资源
最近更新 更多