按“通知”将打开同一活动两次



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

我的代码:

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"

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

您需要

向通知意图添加 FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP,FLAG_ACTIVITY_NEW_TASK 标志。所以改变

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

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

FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP

引用任务和后退堆栈

这是错误。

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

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

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

例如,使用共享Prefences每次都提供不同的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();

最新更新