Firebase Cloud Messaging (FCM) - Launch Activity when user clicks the notification with extras
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...