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

kis*_*idp 6 android android-notifications firebase firebase-cloud-messaging

我在项目中实施了FCM。推送通知按预期方式工作,在收到通知时调用onMessageReceived。当应用程序位于前台时,这是正确的。

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

如何解决这个问题?

编辑:添加代码

我扩展了FirebaseMessagingService类并将其包含在onMessageReceived方法中

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

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

@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());
}
Run Code Online (Sandbox Code Playgroud)

jlb*_*ofh 9

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

<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" />
Run Code Online (Sandbox Code Playgroud)

因此,从清单中删除它并卸载并重新安装应用程序我的问题解决了。

  • 不幸的是,这并没有解决我的问题(我没有否决你的答案)。但是您引导我走上了解决方案的正确道路,并为此感谢您!在我的例子中,我正在做一个 React-Native 应用程序,我使用的是 `zo0r/react-native-push-notification` 包,我遵循了文档,它让你为两者配置 `AndroidManifest.xml` 文件FCM 和 GCM。删除 GCM 部分解决了问题。再次谢谢你 !(我正在对您的答案进行投票,以便它再次为 0 :p (2认同)

小智 5

要手动处理推送通知,请使用FirebaseMessagingServicehandleIntent(intent)。当应用程序处于前台、后台和终止状态时会调用此方法。为了避免重复,不要调用super.handleIntent(intent)。这将防止应用程序处于 BG 或终止状态时自动推送通知。

这对我有用。


bre*_*mes 0

我遇到了完全相同的“重复”问题。这可能只是一种解决方法,因为当应用程序位于前台时我无法收到通知,而不会发生“重复”问题。相反,我实现了一个 WakefulBroadcastReceiver,当将 android:exported 切换为“false”时,它开始表现。

AndroidManifest.xml
    <receiver
        android:name="PACKAGE.MyWakefulBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>

MyWakefulListenerService.java
    public class MyWakefulBroadcastReceiver extends WakefulBroadcastReceiver {

        private final String TAG = "MyWakefulListener";

        public void onReceive(Context context, Intent intent) {
            sendNotification(context);
        }
    }
Run Code Online (Sandbox Code Playgroud)