xamarin.forms - 不要在后台使用 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)
{
}
}
请给我建议。
解决方案
推荐阅读
- c++ - 何时适合重新分配函数参数或返回指向新内存的指针?
- rest - 如何在 Flutter 和 Prestashop 中进行登录
- reactjs - 如何在 babel-plugin-module-resolver 中使用 root 选项?
- android - 如何设置物品重量导航抽屉?
- if-statement - Else-If 命令在执行自己的 ifs 后创建变量
- r - 如何从过滤后的数据框中提取向量
- javascript - 迁移到 Typescript 时无法弄清楚如何使用模式指令
- python - 如何在 Python 中乘以 3D numpy 数组以获得 2D numpy 数组?
- bash - 带有awk的bash中的正则表达式
- c - 在 c 语言的 for 循环中提供输入后,我的程序停止工作