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!!
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.
The "onActivityDestroyed" will get called when the app is closed, so if you can check if the app is in background when it is called (so the app is already closed) you can grep exactly the moment when the app is being closed.
You need to use putExtra(ID_KEY,id)
when you create your Intent
for starting your application, and in your onCreate()
method you can use getIntent().getExtras().getInt(ID_KEY);
to retrieve your passed id integer
.
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.
Intent intent = this.getIntent(); if (intent != null && intent.getExtras() != null && intent.getExtras().containsKey("JOBID") && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) { int jobID = this.getIntent().getExtras().getInt("JOBID"); if (jobID > 0) { } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With