我知道为什么在我没有创建 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/