【问题标题】:How to prevent Notification from clearing all activities in backstack如何防止通知清除后台堆栈中的所有活动
【发布时间】:2022-01-24 12:32:15
【问题描述】:

我正在开发一个带有 GCM 推送通知的 android 应用程序,其中每当通知打开页面时,它都会从 backstack 中清除所有活动。

Intent intent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
long[] vibrate = {0, 100, 200, 300};
Uri notification = RingtoneManager
        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
        PendingIntent.FLAG_UPDATE_CURRENT
                | PendingIntent.FLAG_ONE_SHOT);
Bitmap largIcon = BitmapFactory.decodeResource(this.getResources(),
        R.mipmap.ic_launcher);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        this).setSmallIcon(R.mipmap.small_icon_notification)
        .setContentTitle(getResources().getString(R.string.app_name))
        .setLargeIcon(largIcon)
        .setSound(notification)
        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
        .setContentText(msg).      setSound(notification)
        .setStyle((new NotificationCompat.BigTextStyle()).bigText(msg)) .setAutoCancel(true);
mBuilder.setVibrate(vibrate);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());

【问题讨论】:

  • 您已经在 Manifest 中定义了父级?像这样 android:parentActivityName="MainActivity" 如果不添加此行并测试您的代码它将正常工作
  • 尝试删除自动取消

标签: android push-notification google-cloud-messaging


【解决方案1】:

为了防止 Notification 清除 backstack 中的所有活动,您必须设置一个特殊的 Activity PendingIntent。一个特殊的 Activity 不需要回栈。使用清单设置活动任务选项并通过调用 getActivity() 创建 PendingIntent。

PendingIntent 指定将来要执行的操作。它允许您将未来的 Intent 传递给另一个应用程序,并允许该应用程序执行该 Intent,就好像它具有与您的应用程序相同的权限,无论最终调用 Intent 时您的应用程序是否仍然存在

通过将 PendingIntent 提供给另一个应用程序,您授予它执行您指定的操作的权利,就好像另一个应用程序是您自己一样(具有相同的权限和身份)。

下面的代码sn -p演示了这个过程:

// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

// Creates an Intent for the Activity
Intent notifyIntent =
    new Intent(new ComponentName(this, ResultActivity.class));

// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
    Intent.FLAG_ACTIVITY_CLEAR_TASK);

// Creates the PendingIntent
PendingIntent notifyIntent = PendingIntent.getActivity(this,0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);

// Puts the PendingIntent into the notification builder
builder.setContentIntent(notifyIntent);

// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
mNotificationManager.notify(id, builder.build());

这是一个有用的链接 How to Setup a Special Activity PendingIntent: http://developer.android.com/training/notify-user/navigation.html

【讨论】:

    【解决方案2】:

    来自

    PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
        PendingIntent.FLAG_UPDATE_CURRENT
                | PendingIntent.FLAG_ONE_SHOT);
    

    到

    PendingIntent contentIntent = stackBuilder.getPendingIntent(99,
        PendingIntent.FLAG_UPDATE_CURRENT
                | PendingIntent.FLAG_ONE_SHOT);
    

    只需更改 requestCode 即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多