【发布时间】:2011-11-13 14:07:02
【问题描述】:
一般来说,当我在通知栏上有通知消息并单击它时。它打开该消息的注册应用程序。
在 Startup 的 Activity 上,如何判断 App 是否打开?
更好的是如何在 OnCreate() 方法上检索通知的 id?
更新:来自@Ovidiu - 这是我的 putExtra 推送代码
Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, Startup.class);
notificationIntent.putExtra("JOBID", jobId);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.contentIntent = contentIntent;
mNotificationManager.notify(jobId, notification);
主活动“Startup.java”代码是
Intent intent = this.getIntent();
if (intent != null && intent.getExtras() != null && intent.getExtras().containsKey("JOBID")) {
int jobID = this.getIntent().getExtras().getInt("JOBID");
if (jobID > 0) {
}
}
intent.getExtras() 总是返回 null。结果,我需要通过 PendingIntent.FLAG_ONE_SHOT 。现在传了!!
【问题讨论】:
-
"需要通过 PendingIntent.FLAG_ONE_SHOT" - 真的很有帮助。但是您需要检查另一件事 - 如果此 Activity 来自历史记录。在下面检查我的答案。
-
FLAG_ONE_SHOT 导致应用程序在单击通知时仅启动一次,这可能不是您想要的,以防来自后台服务的粘性通知。在那种情况下,FLAG_UPDATE_CURRENT 对我有用。