当应用程序处于后台时,FCM 显示重复通知

编程入门 行业动态 更新时间:2024-10-12 03:17:56
本文介绍了当应用程序处于后台时,FCM 显示重复通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在我的项目中实施了 FCM.推送通知按预期工作,收到通知时调用 onMessageReceived.当应用程序在前台时是这样.

I implemented FCM in my project. Push notification is working as expected, onMessageReceived is called when a notification is received. This is true when app is in the foreground.

但是,当应用程序在后台时,系统托盘总是在一个到达时显示重复的通知(例如,当收到通知 A 时,系统托盘显示 2 个通知 A)

However, when the app is in the background, the system tray always display a duplicate notification when one arrives (e.g. when Notification A is received, system tray display 2 Notification A)

如何解决这个问题?

添加代码

我扩展了 FirebaseMessagingService 类,并在 onMessageReceived 方法中有这个

I extended FirebaseMessagingService class and have this in the onMessageReceived method

这是项目中我使用 NotificationManager 的唯一部分.

This is the only part in the project where I used NotificationManager.

另外,我尝试在此方法上添加日志.当应用程序在前台时调用 onMessageReceived.应用在后台时不会被调用

Also, I tried to add a log on this method. onMessageReceived is called when app is in foreground. It doesn't get called when app is in background

@Override public void onMessageReceived(RemoteMessage remoteMessage) { RemoteMessage.Notification notification = remoteMessage.getNotification(); Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); String title = notification.getTitle(); String message = notification.getBody(); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); }

推荐答案

同样的问题.我修改了 AndroidManifest.xml,因为我正在像这样请求对旧 GCM 的权限...

Same problem. I modified AndroidManifest.xml because i was requesting permissions to old GCM like this...

<uses-permission android:name="mypackage.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="mypackage.permission.C2D_MESSAGE" android:protectionLevel="signature" />

所以,从清单中删除它并卸载并重新安装应用程序,我的问题就解决了.

So, removing it from manifest and uninstalling and reinstalling the app my problem was solved.

更多推荐

当应用程序处于后台时,FCM 显示重复通知

本文发布于:2023-11-27 10:53:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637778.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   台时   通知   FCM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!