Check or catch warning failed to post notification on channel

Viewed 55

I know why this warning appears in my case I didn't create channel but it is not the question , what I want to know is how to catch it or check if channel exist it in the code in order to handle it before publishing the notification.

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

1 Answers

You can use NotificationManager.GetNotificationChannel and it will return an instance of the NotificationChannel if it exists or a null if it does not.

Quick Example:

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
}
Related