c# - 检查或捕捉警告未能在 channel 上发布通知

标签 c# android xamarin xamarin.forms android-notifications

我知道为什么在我没有创建 channel 的情况下会出现此警告,但这不是问题所在 ,我想知道的是如何捕获它或检查 channel 是否存在于代码中以便在发布通知之前处理它。

"Developper warning for package "com.app" failed to post notification on channel "channel1" see log for more details" thanks.

最佳答案

您可以使用 NotificationManager.GetNotificationChannel,它将返回 NotificationChannel 的实例(如果存在)或 null(如果不存在)。

简单示例:

using (var notificationManager = NotificationManager.FromContext(ApplicationContext))
{
    var channelName = "SomeChannelName";
    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
    {
        NotificationChannel channel = notificationManager.GetNotificationChannel(channelName);
        if (channel == null)
        {
            channel = new NotificationChannel(channelName, channelName, NotificationImportance.Low)
            {
                LockscreenVisibility = NotificationVisibility.Public
            };
            channel.SetShowBadge(true);
            notificationManager.CreateNotificationChannel(channel);
        }
    }
    // build your notification
}

关于c# - 检查或捕捉警告未能在 channel 上发布通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65269195/

相关文章:

c# - 如何使用 Entity Framework Code First Fluent API 仅在连接表上级联删除

c# - 在 C# 中通过引用传递数组中的结构对性能有何影响

c# - 在 Xamarin.Android 中的日历之间转换日期

android - Android 中的 System.exit()

c# - Xamarin Forms 和 MVVM 架构中的 map

c# - 从方法返回后变量值不会改变

android - 如何获取 ActionBar 图标/ Logo 上的 OnClick

android - 识别哪个 Android 应用程序正在与我的应用程序引擎应用程序联系

Xamarin.Forms 曲棍球应用程序错误 CrashManager.Register

c# - Xamarin Android 中选项卡更改前的验证