如何从通知消息中判断Android App是否打开?
一般来说,当我在通知栏上看到通知消息并单击它时。它会打开该消息的注册应用程序。
在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 。现在已经传下来了!!
In general, when i have notification message on the notification bar and click on it. It open the registered App for that message.
On Startup's Activity, How to determine if App is open from it?
and more better is How to retrieve the notification's id on the OnCreate() method?
Update: from @Ovidiu - here is my code to putExtra to push
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);
and on Main Activity "Startup.java" code is
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() always return null. Turn out, I need to pass PendingIntent.FLAG_ONE_SHOT . It is now passed along!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您创建
Intent
来启动应用程序时,您需要使用putExtra(ID_KEY,id)
,并且在onCreate()
方法中您可以使用getIntent().getExtras().getInt(ID_KEY);
检索您传递的 IDinteger
。You need to use
putExtra(ID_KEY,id)
when you create yourIntent
for starting your application, and in youronCreate()
method you can usegetIntent().getExtras().getInt(ID_KEY);
to retrieve your passed idinteger
.Start Activity 代码应该是这样的,否则一旦来自 GCM 通知后,此后每次 Activity 来自最近列表时,它都会说它来自 GCM 通知,这是错误的。
The Start Activity code would be like this, otherwise after once it comes from GCM notification, from then every time the Activity comes from the Recent list, it will say it comes from GCM notification, which is wrong.
我遇到了同样的问题,我不明白为什么我必须使用 putExtra 方法......
所以我是这样解决的:当您收到通知并点击它时,应用程序将打开(通常它会打开应用程序的主要活动),并且在附加功能中,您可以找到有关该通知的一些信息。
您可以将键/值参数添加到发送到注册设备的通知中。这些参数将被添加到意图附加中。
因此,您可以这样做:在通知中添加一个代表您的通知 ID 的参数。
例如“messageId”-> “abc”,其中 abc 是您的通知标识符。
然后,在您的主要活动中,您可以执行以下操作:
您将检索通知的 id。
因此,您可以使用此信息来从数据库或其他操作获取通知。
I Had the same problem, and I don't understand why I have to use the putExtra method...
So I solved like this: when you receive a notification and tap on it, the app will open (usually it opens the app main activity) and, in the extras, you can find some information about that notification.
You can add key/value params to the notifications you're sending to registered devices. These params will be added to the intent extras.
So you can act like this: in your notification, add a parameter that represents your notification id.
For example "messageId" -> "abc", where abc is your notification identifier.
Then, in your main activity, you can do:
And you will retrieve the id of the notification.
So you can use this information to, for example, get the notification from your db or other operations.
还有一个案例。
当您启动带有 Firebase 推送通知的应用程序时,在意图的附加部分中有包含
{ key : "from" , value : gcm_defaultSenderId }
的记录这样,如果此记录可用,我可以知道我的应用程序是通过 firebase 推送通知启动的,否则我通过键将自己的值放入 PendingIntent extras 中:“from”
There is also a case.
While you're launching app owing firebase push notification, in the extras of intent there is record containing
{ key : "from" , value : gcm_defaultSenderId }
In this way, if this record is available i can know was my app launched by firebase push notification, otherwise i put my own values into PendingIntent extras by key : "from"