【发布时间】:2015-03-27 10:05:01
【问题描述】:
我正在我的应用中开发通知,我遇到了一些让我发疯的待处理意图问题。
正常流程:我的应用有启动器 Activity(Activity A,singleTop),它显示 Splash,然后启动 Activity B(也是 singleTop)。
通知: 当应用程序处于后台时,我会在通知栏上显示一条通知,单击时会通过 PendingIntent 打开我的应用程序的启动器活动。这个 PendingIntent 指向 Activity A (singleTop)。但是在这种情况下,它不是打开 Activity A,而是将 Activity B 带到前台,但没有调用 onNewIntent()(而是调用 onResume()),所以我无法检索通知 Intent 的额外内容并显示信息,因为此 Activity B.getIntent() 检索第一次打开该 Activity 的旧 Intent。
请大家帮我解释一下这个问题吗?
这就是我设置 PendingIntent 的方式:
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TYPE, "Notification");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_MESSAGE, "Message");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TITLE, "title");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
已编辑: 根据@Woodi_333 给出的答案,创建待处理意图的代码如下。
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TYPE, "Notification");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_MESSAGE, "Message");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TITLE, "title");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
【问题讨论】:
-
为什么要调用onnewIntent?正如您所说的应用程序在后台,您的活动已被破坏。所以点击通知会启动新的活动。
-
SplashActivity.class -> 将其更改为您想要点击通知的导航活动。
-
@PankajKumar 我认为它应该调用 onNewIntent,因为活动在后台,而不是被破坏。因此,当我通过 PendingIntent 启动一个新意图时,它应该按照文档中的说明调用它。但是,我可能理解错了。
-
@SurenderKumar 我必须导航到 SplashActivity,因为我之前需要执行一些操作,如果应用程序不在后台,它应该以正确的顺序启动应用程序。
标签: android android-activity android-pendingintent