Notifications are not delivering xamarin android

后端 未结 2 1371
野的像风
野的像风 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:09

    Following this guid helped me:

    https://docs.microsoft.com/sv-se/xamarin/android/app-fundamentals/notifications/local-notifications-walkthrough

    I think the reason I didn't see any notifications was that I didn't have an icon. After adding an icon to the notification, everything worked.

    Result:

    [Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
    public class GcmNotificationService : GcmListenerService
    {
        //More information on how to set different things from notification can be found here
        //https://docs.microsoft.com/sv-se/xamarin/android/app-fundamentals/notifications/local-notifications
    
        public override void OnMessageReceived(string from, Bundle data)
        {
            var message = data.GetString("message");
            if (!string.IsNullOrEmpty(message))
            {
                if (!NotificationContextHelper.Handle(message))
                    SendNotification(message);
            }
        }
    
        private void SendNotification(string message)
        {
            var builder = new Notification.Builder(this)
                    .SetContentTitle("Title")
                    .SetContentText(message)
                    .SetSmallIcon(Resource.Drawable.notification_test)
                    .SetVisibility(NotificationVisibility.Public);
    
            var notification = builder.Build();
            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
            notificationManager.Notify(0, notification);
        }
    }
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
提交回复
热议问题