android - 当应用程序处于前台时,FCM 推送通知正在替换
问题描述
有一些用于计划任务的 Web 应用程序。如果添加了一项任务,通知将发送到应用程序,其中包含一些任务详细信息。如果添加了第二个任务,则第一个通知将替换为第一个通知。仅显示一个通知,即第二个通知。
当应用程序在后台时,会显示 2 个通知。
但是一个应用程序在前台只显示一个通知。请帮我。
任何帮助,将不胜感激。
这是我在应用程序处于前台时的通知代码。
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())){
Intent intent = new Intent(Config.PUSH_NOTIFICATION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra("Plan","fromFCM");
broadcastManager.sendBroadcast(intent);
Bitmap bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.notify_app_icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBulder = new NotificationCompat.Builder(this);
notificationBulder.setContentTitle(notificationData.getTitle());
notificationBulder.setContentText(notificationData.getTextMessage());
notificationBulder.setSmallIcon(R.drawable.notify_app_icon);
notificationBulder.setLargeIcon(bitmapIcon);
notificationBulder.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationData.getTextMessage()));
notificationBulder.setAutoCancel(true);
//notificationBulder.setSmallIcon(R.mipmap.ic_launcher);
notificationBulder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(notificationData.getId(), notificationBulder.build());
/* Plan_Details plan_details = new Plan_Details();
plan_details.refreshPlan();*/
}
解决方案
Android 使用通知 ID 来区分通知。因此,它无法区分通知并显示多个通知。
tag
在通知负载中使用参数。
{
"notification" : {
"title" : "Notification Title",
"body" : "Notification Body",
"tag" : "your_unique_tag"
}
}
Identifier used to replace existing notifications in the notification drawer.
If not specified, each request creates a new notification.
If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.
推荐阅读
- javascript - 从javascript中的数组数组打印isduplicate
- python - 有没有办法在 DF 中找到一个列索引,它的整个值与使用 python 的另一列匹配
- asp.net-core - 全局错误处理程序,包括模型绑定器错误
- arrays - 使用 VBA 从集合的 JSON 集合创建数组
- javascript - 是否有一个javascript函数用于从一组集合中找到第一个包含一个元素的集合(如果该组存在这样的集合)?
- c# - 在 xamarin 表单上构建错误:Android
- scala - Scala 3 - 在一阶类型上提取包装器和 InverseMap 元组
- swift - 是否存在 firstIndex 在 Swift 中不返回可选项的情况?
- python - 运行 spark-submit 时没有模块错误
- azure - Azure API 管理 - 如何使用 Azure Cli 添加 API 和操作?