Я использую формы xamarin для разработки приложений. Я использую push-уведомление 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)
{
}
}
Пожалуйста, дайте мне предложения для этого.