按下通知会打开相同的 activity 两次

pressing on notification opens the same activity twice

如果我有一个 activity 被用户关闭了(用户按下了主页,所以该应用程序仍在应用程序堆栈中) 然后他收到通知,当他按下它时我开始 activity 现在相同的 activity 被打开了两次。 我怎样才能防止这种情况发生?

我的代码:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
        new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager mNotificationManager = (NotificationManager)
        this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setLights(GCMSIntentService.PURPLE, 500, 500)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentText(msg);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
android:launchMode="singleTask"

在清单中或者, 将其用作您的意图的标志。

by android:launchMode="singleTask",如果 activity 的实例已经存在于单独的任务中,系统将通过调用其 onNewIntent() 方法将意图路由到现有实例,而不是创建一个新实例并向意图添加标志

FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP

参考task and back stack

您需要在通知中添加 FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP,FLAG_ACTIVITY_NEW_TASK 标志 intent.So 更改

 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

Intent notificationIntent = new Intent(this, MainActivity.class);

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);


PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , PendingIntent.FLAG_UPDATE_CURRENT);

这是错误。

每次都必须更改通知功能 ID。你每次都给它 1。这就是错误。

mNotificationManager.notify(1, mBuilder.build());

每次都给一个不同的ID。它会像一个魅力。

例如,每次使用 sharedPrefences 提供不同的 id。

检查这个,

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationManager mNotificationManager = (NotificationManager)
                    this.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.logo)
                            .setContentTitle("Title")
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(msg))
                            .setAutoCancel(true)
                            .setLights(GCMSIntentService.PURPLE, 500, 500)
                            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                            .setContentText(msg);

            mBuilder.setContentIntent(contentIntent);

 SharedPreferences sp = context.getSharedPreferences("randomidfornotification", Activity.MODE_PRIVATE);
        myIntValue = sp.getInt("notificationid", 0);

mNotificationManager.notify(1, mBuilder.build());

SharedPreferences sp1 = context.getSharedPreferences("randomidfornotification", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp1.edit();
        editor.putInt("notificationid", myIntValue+1);
        editor.apply();