android - 按下通知会打开相同的 Activity 两次

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

如果我有一个被用户关闭的 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"

在 list 中或者, 将其用作您的 Intent 的标志。

关于android - 按下通知会打开相同的 Activity 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29627662/

相关文章:

android - 点击 Facebook Messenger 聊天项目动画

android - 返回应用时的静态变量null

android - 以编程方式从 Activity/服务/接收器更新小部件

android - Activity 到 Activity 的交流

ios - iOS 中未收到 FCM 通知

android - 如何将数据从服务器发送到Android?

android - 如何从 https 服务器获得在 android 中有证书异常的响应?

android - Play 商店显示我的应用程序不受 Nexus 7 和其他支持

javascript - 在 android 上的样式选择上触发默认事件

javascript - Chrome 应用程序弹出通知