Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

c# - Notifications are not delivering xamarin android

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...