16

I'm trying to open a specific activity when the user clicks the notification, when the app is in the background, with some extra parameters. I'm using the click_action and it's working fine, the app opens the desired Activity.

Now I need the server to pass an extra parameter, an id, to this Activity so I can present the desired details associated with the notification. Like an e-mail application, that when we click on the notification opens the details of that specif email.

How can I do this?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
m4n3k4s
  • 1,293
  • 1
  • 19
  • 21
  • Can you share some code? I have a similar thing in my app but I'm using GCM. In the service that receives the notifications, I simply add a string extra to the Intent and use that to see which fragment I should open. – Vucko Jun 01 '16 at 11:07
  • I don't need any help, my app works :) good for you that you've fixed it – Vucko Jun 01 '16 at 12:14
  • Have you solved it? I am fetching same issue. I starting the targetActivity but didn't get extras from intent. Have you any solution? – Md. Sajedul Karim Aug 18 '16 at 15:26

2 Answers2

39

Ok, I have found the solution.

This is the json that I'm sending from the server to the app

{
  "registration_ids": [
    "XXX",
    ...
  ],
  "data": {
    "id_offer": "41"
  },
  "notification": {
    "title": "This is the Title",
    "text": "Hello I'm a notification",
    "icon": "ic_push",
    "click_action": "ACTIVITY_XPTO"
  }
}

At the AndroidManifest.xml

<activity
    android:name=".ActivityXPTO"
    android:screenOrientation="sensor"
    android:windowSoftInputMode="stateHidden">
    <intent-filter>
        <action android:name="ACTIVITY_XPTO" />        
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

When the app is closed or in background and the user clicks on the notification it opens my ActivityXPTO, to retrieve the id_offer I only need to do

public class ActivityXPTO extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...

        String idOffer = "";

        Intent startingIntent = getIntent();
        if (startingIntent != null) {
            idOffer = startingIntent.getStringExtra("id_offer"); // Retrieve the id
        }

        getOfferDetails(idOffer);
    }

    ...
}

That's it...

m4n3k4s
  • 1,293
  • 1
  • 19
  • 21
  • 2
    Also see http://stackoverflow.com/questions/37554274/open-app-on-firebase-notification-received-fcm/37626817#37626817 – Frank van Puffelen Jun 21 '16 at 00:01
  • Thanks for your nice answer. Using your way I starting the targetActivity but didn't get extras from intent. Have you any solution? – Md. Sajedul Karim Aug 18 '16 at 15:30
  • Hi @Md.SajedulKarim, do you have an example of the json you are sending from the server? You have to add the extras inside the "data" object of your json. – m4n3k4s Aug 22 '16 at 14:05
  • this worked for me too. if i want to open a particular page then what i have to do. see my question https://stackoverflow.com/questions/44797486/open-specific-page-using-firebase-api – Harish Mahajan Jul 04 '17 at 11:28
0

Add addition information to Intent that you use to start Activity, and in activity in method onCreate use getIntent().getExtras() to use them. For example:

Starting activity:

Intent intent = new Intent(context, TargetActivity.class);
Bundle bundle = new Bundle();
bundle.putString("extraName", "extraValue"); 
intent.putExtras(bundle);
startActivity(intent); 

In activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = getIntent().getExtras();
    String value = bundle.getString("extraName");
    ....
}
  • 1
    It's not me who is starting the Activity. This is taken care by the system. The app is in background or closed. – m4n3k4s Jun 01 '16 at 10:30