Notifications are not delivering xamarin android

后端 未结 2 1416
野的像风
野的像风 2020-12-22 05:32

When the notification is recieved following code is handling the message:

private void SendNotification(string message)
{
    var intent = new Intent(this, t         


        
2条回答
  •  难免孤独
    2020-12-22 06:26

    With the newer Android APIs, they now require a notification channel (NotificationChannel) to be used. You can do fairly easily by using NotificationCompat from the Android support library and only creating the channel if you are on Oreo or later.

    NotificationCompat w/ Channel Example:

    using (var notificationManager = NotificationManager.FromContext(ApplicationContext))
    {
        var channelName = GetText(Resource.String.notificationChannelNormal);
        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            NotificationChannel channel = null;
            #if !DEBUG
            channel = notificationManager.GetNotificationChannel(channelName);
            #endif
            if (channel == null || resetChannel)
            {
                channel = new NotificationChannel(channelName, channelName, NotificationImportance.Low)
                {
                    LockscreenVisibility = NotificationVisibility.Public
                };
                channel.SetShowBadge(true);
                notificationManager.CreateNotificationChannel(channel);
            }
            channel.Dispose();
        }
        Bitmap bitMap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_launcher);
        var notificationBuilder = new NotificationCompat.Builder(ApplicationContext)
                                                        .SetContentTitle(title)
                                                        .SetContentText(message)
                                                        .SetSmallIcon(Resource.Drawable.ic_stat_notification_network_locked)
                                                        .SetLargeIcon(bitMap)
                                                        .SetShowWhen(false)
                                                        .SetChannelId(channelName)
                                                        .SetContentIntent(pendingIntent);
        return notificationBuilder.Build();
    }
    

提交回复
热议问题