Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

private void SendNotification(string message)
{
    var intent = new Intent(this, typeof(MainActivity));
    intent.AddFlags(ActivityFlags.ClearTop);
    var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

    var notificationBuilder = new Notification.Builder(this)
            .SetContentTitle("GCM Message")
            .SetContentText(message)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent);

     var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
     notificationManager.Notify(0, notificationBuilder.Build());
}

But nothing shows. I'm debugging it it that would make any difference? When I go to settings on the app on the device, "Show notifications" is checked.

Comment 1:

using (var notificationManager = NotificationManager.FromContext(ApplicationContext))
{
    var title = "Title";
    var channelName = "TestChannel"
    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
    {
        NotificationChannel channel = null;
        if (channel == null)
        {
            channel = new NotificationChannel(channelName, channelName, NotificationImportance.Low)
            {
                LockscreenVisibility = NotificationVisibility.Public
            };
            channel.SetShowBadge(true);
            notificationManager.CreateNotificationChannel(channel);
        }
        channel.Dispose();
    }
    var bitMap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.notification_template_icon_bg);
    var notificationBuilder = new NotificationCompat.Builder(ApplicationContext)
                                                            .SetContentTitle(title)
                                                            .SetContentText(message)
                                                            .SetLargeIcon(bitMap)
                                                            .SetShowWhen(false)
                                                            .SetChannelId(channelName);

    var notification = notificationBuilder.Build();
    notificationManager.Notify(0, notification);
}

Runnig that code, no errors but nothing shows up.

Thanks for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...