我目前有一个通知显示在通知栏中,当用户单击它时,它应该将他们带到我的应用程序的根 Activity。
当用户从应用程序屏幕或主屏幕按下应用程序图标时,我在 list 中使用了 android:finishOnTaskLaunch ="true"和 android:clearTaskOnLaunch="true"来实现此目的。用户始终会按预期进入根 Activity。
但是,当我使用通知中的以下代码时,用户会被带到他们所在的最后一个屏幕,而不是 StartActivity 的根 Activity 。
有人知道我做错了什么吗?
Intent notificationIntent = new Intent(this, StartActivity.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), getString(R.string.notification_app_name), notificationText, contentIntent);
mNotificationManager.notify(1, notification);
添加这些标志有帮助,但重新启动后仍然不起作用:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
当我重新启动应用程序时,我设置了一个正在运行的服务,该服务会弹出通知,当我这样做时,通知会恢复到原来的不良行为。
已解决:
通过将此标志添加到我的通知的 contentIntent 中解决了该问题 - Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED。
这里是一个链接,其中包含有关该地区的一些精彩信息Google dev group
最佳答案
尝试这 2 个属性
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
关于Android - 通知 PendingIntent 转到错误的 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257208/