Android 由 PendingInent 创建的任务无法再次启动另一个具有 pendingIntent 的 activity

Android Task Created by PendingInent cannot start another activity with pendingIntent again

我有 2 个 Activity:Main(将通过警报管理器安排通知)和 Message(处理用户导航或通知)。 Message 上没有设置启动模式和标志。消息被用户导航或通知打开。

工作正常的正常控制流程:

(Home Screen) -> (user) -> Main的新任务 -> (user) -> new Message -> (notification) -> new Message -> (notification) ->新消息 -> (用户) -> 新消息

当应用程序未加载且通过消息通知启动新任务时。流量还可以:

(主屏幕)->(通知)->消息的新任务->(用户)->新消息

点击第二个通知时出现问题,没有正确处理。第二次通知需要一条新消息。

(Home Screen) -> (notification) -> Message 的新任务 -> (notification) -> X nothing now but expect new Message

最后一个通知挂起的意图无法启动消息(没有 OnCreate,没有 OnNewIntent)。也没有新任务开始处理它。如果任务在后台,通知挂起意图会将任务带到前台(但没有新的activity)。结果是,对于由挂起的 Intent 启动的任务,任何其他挂起的 Intent 都不会创建新的 activity。不确定它是否与未决的意图安全问题或其他问题有关。

当任务以挂起的意图启动时,我如何开始新的 activity 带有通知挂起的消息?任何帮助将不胜感激。

谢谢,

尼克

代码是用Xamarin写的,供参考:

闹钟设置代码,上下文为Activity上下文

    Intent intentScheduleReminder = new Intent(context, typeof(BroadcastReceiver));
    intentScheduleReminder.SetAction(PlatformConstants.NotificationActionShow);
    intentScheduleReminder.PutExtra(PlatformConstants.ScheduleReminderId, scheduleReminder.Id);
    PendingIntent intentAlarm = PendingIntent.GetBroadcast(context, scheduleReminder.Id, intentScheduleReminder, PendingIntentFlags.CancelCurrent);

    Java.Util.Calendar calendar = DroidUtility.ConvertDateTimeToCalendar(scheduleReminder.ScheduleDateTime);
    AlarmManager alarmManager = (AlarmManager)Application.Context.GetSystemService(Context.AlarmService);
    alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, calendar.TimeInMillis, intentAlarm);

发送通知的代码。 Pending Intent 是使用 BroadcastReceiver.OnReceive

的上下文创建的
    var builder = new NotificationCompat.Builder(Application.Context,  PlatformConstants.NotificationChannelIdBillReminder)
        .SetContentTitle(appNotification.Title)
        .SetContentText(appNotification.Message)
        .SetSmallIcon(Resource.Drawable.ic_appicon_24dp)
        .SetLargeIcon(largeIconBitmap)
        .SetAutoCancel(true)
        .SetOnlyAlertOnce(true);
    Intent intent = new Intent((Context)BroadcastContext, typeof(InfiniteScrollingParentFsActivity));
    intent.PutExtra(……
    PendingIntent pendingIntent = PendingIntent.GetActivity((Context)BroadcastContext, notificationId, intent, 0);
    builder.SetContentIntent(pendingIntent);
    Notification notification = builder.Build();
    NotificationManager notificationManager = (NotificationManager)Application.Context.GetSystemService(Context.NotificationService);
    notificationManager.Notify(notificationId, notification);

仔细阅读the documentation for PendingIntent。特别是这部分:

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

您的通知始终触发相同的 Intent:

Intent intent = new Intent((Context)BroadcastContext, typeof(InfiniteScrollingParentFsActivity));
intent.PutExtra(……
PendingIntent pendingIntent = PendingIntent.GetActivity((Context)BroadcastContext, notificationId, intent, 0);

所以它总是会重新打开相同的 Activity 如果它存在。

您可能想让您的意图独一无二,这样每个意图都代表一条消息。您可以通过为意图的 data 字段设置唯一值来做到这一点:

Intent intent = new Intent((Context)BroadcastContext, typeof(InfiniteScrollingParentFsActivity));
intent.PutExtra(……

intent.SetData(something that uniquely ids this message here) <----

PendingIntent pendingIntent = PendingIntent.GetActivity((Context)BroadcastContext, notificationId, intent, 0);