Android - 通知 PendingIntent 转到错误的活动?
我目前有一个通知显示在通知栏中,当用户单击它时,它应该将他们带到我的应用程序的根活动。
我用过 android:finishOnTaskLaunch ="true" & android:clearTaskOnLaunch="true" 在我的清单中,以在用户从应用程序屏幕或主屏幕按下应用程序图标时实现此目的。用户始终会按预期进入根 Activity。
但是,当我使用通知中的以下代码时,用户将被带到他们所在的最后一个屏幕,而不是根活动,即 StartActivity。
有谁知道我做错了什么?
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 开发小组
I currently have a notification that displays in the notification bar and when the user clicks it it should bring them to the root Activity of my application.
I have used android:finishOnTaskLaunch ="true" & android:clearTaskOnLaunch="true" in my manifest to achieve this when the user presses the app icon from the apps screen or the home screen. The user is always taken to the root activity as expected.
However when I use the following code from my notification the user gets taken to the last screen they were on rather than the root activity which is StartActivity.
Does anyone know what I'm doing wrong?
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);
Adding these flags helps however it still doesn't work after a restart:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
When I restart my application I set a service running which pops up the notification and when I do this the notification reverts back to the old undesired behavior .
SOLVED:
Solved the issue by adding this flag to the contentIntent of my notification - Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED.
Here is an link with some excellent information on the area Google dev group
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这 2 个属性
try this 2 properties