user13558367
user13558367

Reputation:

Check or catch warning failed to post notification on channel

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.

Upvotes: 3

Views: 93

Answers (1)

SushiHangover
SushiHangover

Reputation: 74184

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
}

Upvotes: 1

Related Questions