首页 > 解决方案 > 不要在后台使用 xamarin 表单从 Firebase 上的通知栏中刷出通知消息

问题描述

我正在使用 xamarin 表单进行应用程序开发。我正在为我的应用程序使用 firebase 推送通知,并使用以下代码接收通知。当我的应用程序在前台时,通知不应该被刷出,但是当应用程序在后台时,我们可以滑动通知。请参阅下面的代码我正在使用什么:

public override void OnMessageReceived(RemoteMessage message)
        {
            try
            {
                Android.Util.Log.Debug(TAG, "From: " + message.From);
                Android.Util.Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
                SendNotifications(message);
            }
            catch (System.Exception ex)
            {
                Logs.LogCreate("OnMessageReceived Exception" + ex.Message);
                Crashes.TrackError(ex);
            }
        }

        public void SendNotifications(RemoteMessage message)
        {
            try
            {
                NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);

                var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"\d+").Value);
                int id = new Random(seed).Next(000000000, 999999999);

                var push = new Intent();

                var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0,
                        push, PendingIntentFlags.CancelCurrent);

                NotificationCompat.Builder notification;
                if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                {
                    var chan1 = new NotificationChannel(PRIMARY_CHANNEL,
                       new Java.Lang.String("Primary"), NotificationImportance.High);
                    chan1.LightColor = Color.Green;

                    manager.CreateNotificationChannel(chan1);

                    notification = new NotificationCompat.Builder(this, PRIMARY_CHANNEL).SetOngoing(true);
                }
                else
                {
                    notification = new NotificationCompat.Builder(this);
                }

                notification.SetContentIntent(fullScreenPendingIntent)
                         .SetContentTitle(message.GetNotification().Title)
                         .SetContentText(message.GetNotification().Body)
                         .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon))
                         .SetSmallIcon(Resource.Drawable.icon_transparent)
                         .SetStyle((new NotificationCompat.BigTextStyle()))
                         .SetPriority(NotificationCompat.PriorityHigh)
                         .SetColor(0x9c6114)
                         .SetAutoCancel(true)
                         .SetOngoing(true);

                manager.Notify(id, notification.Build());

            }
            catch (System.Exception ex)
            {
            }
        }

请给我建议。

标签: xamarin.formsfirebase-cloud-messaging

解决方案


推荐阅读