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

7
我在我的项目中实现了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());
}

1
展示如何构建和显示通知。 - nipun.birla
1
当应用程序在后台运行时,如果它采用默认通知格式,则系统会自动生成通知。 - Vivek Mishra
@VivekMishra 这是说系统会生成两个相同的通知吗?还是说它会接收到两个通知?我很困惑,为什么当应用程序在前台时,我只收到了一个通知,但当在后台时,它突然变成了重复的。 - kishidp
大家好,你们解决了这个问题吗?我也遇到了同样的问题。当应用程序在前台接收一条通知时,如果应用程序在后台,则系统托盘会显示两条通知。你能帮我解决这个问题吗? - sssvrock
我也遇到了同样的问题。有人有解决方案吗? - abdulec90
显示剩余3条评论
3个回答

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" />

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

3
很不幸,这对我并没有解决问题(我没有给你的回答投反对票)。但是,您指导我找到了解决方案的正确路径,感谢您!在我的情况下,我正在做一个React-Native应用程序,并且我正在使用 zo0r/react-native-push-notification 包,我遵循了文档,使您配置 AndroidManifest.xml 文件以适用于 FCM 和 GCM。删除 GCM 部分解决了该问题。再次感谢!(我为您的答案点赞,这样它将再次变为0 :p) - ZedTuX

5
要手动处理推送通知,请使用 FirebaseMessagingServicehandleIntent(intent) 方法。当应用程序处于前台、后台和关闭状态时,此方法将被调用。为避免重复,请不要调用 super.handleIntent(intent)。这将防止在应用程序处于后台或关闭状态时自动推送通知。
这对我很有效。

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);
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接