26

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!!

Jirapong
  • 24,074
  • 10
  • 54
  • 72
  • "need to pass PendingIntent.FLAG_ONE_SHOT" - was really helpful. But you need to check another thing - if this Activity is coming from history. Check my answer below. – Khobaib May 10 '14 at 09:30
  • FLAG_ONE_SHOT causes the app to be launched only once when clicking the notification, which might not be what you want in case of a sticky notification from a background service. In that situation, FLAG_UPDATE_CURRENT worked for me. – Jean-Philippe Sep 02 '19 at 15:15

4 Answers4

24

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.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • 3
    Beware that unless your new intent returns .equals() == false compared to the last intent used, the last intent will be used. Extras are not considered in .equals, so you need to set some unique identifier in setAction or similar. – RunLoop Apr 02 '14 at 07:08
  • 3
    What if the notification came in from Google cloud??? getIntent() always returns null in my case. – Josh Jan 25 '18 at 09:08
9

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) {

    }
}
Khobaib
  • 1,577
  • 3
  • 21
  • 29
3

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:

if (getIntent().getExtras().keySet().contains("messageId")) {
    // you opened the app from a notification
    String messageId = getIntent().getStringExtra("messageId");
    // do domething...
} else {
    // you opened the app normally
    // do domething...
}

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.

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
smukamuka
  • 1,442
  • 1
  • 15
  • 23
  • 1
    How do you suggest to "add key/value params to the notifications" without using putExtra? – auspicious99 May 02 '20 at 08:46
  • You're confused between foreground notifications and remote background notifications. Using "putExtra" is for when your android app has created the notification banner itself - you don't get the chance to do that if your app is in the background when a firebase notification arrives, so your server needs to add the key/value params, which basically means your server is doing the "putExtra" call. Note that if you don't have code to handle the notification and create the banner in your app, no banner will appear if a notification arrives while your app is in the foreground. – Niall Feb 17 '23 at 09:12
3

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"

VadynVadyn
  • 71
  • 5