【问题标题】:pressing on notification opens the same activity twice按下通知会打开相同的活动两次
【发布时间】:2015-04-14 12:44:02
【问题描述】:

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

我的代码:

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

【问题讨论】:

  • 你可以使用intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NO_HISTORY);

标签: android android-activity google-cloud-messaging android-pendingintent


【解决方案1】:
android:launchMode="singleTask"

在清单中或者, 用它作为你意图的标志。

【讨论】:

  • 有没有办法重新创建重新启动的活动? onCreate 应该被调用
  • 重启,你可以在onNewIntent方法中通过notification和handle的方式发布intent,然后根据新的intent进行操作(在你的情况下刷新)
【解决方案2】:

您需要在通知意图中添加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);

【讨论】:

    【解决方案3】:

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

    FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP

    参考task and back stack

    【讨论】:

    • 有没有办法重新创建重新启动的活动? onCreate 应该被调用
    • 您可以将 onCreate 中的任何代码移动到 onResume。这也将解释创建和恢复。参考this chart
    【解决方案4】:

    这是错误。

    每次都必须更改通知函数 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多