Reputation: 4313
I have a question regarding receiving a Firebase message with both Notification and Data payload. The documentation says the Data will arrive "in the extras of the intent".
My question is which Intent (or Activity)? There will be a screen where the user left off when switching the app to the background. So, do I need to try to retrieve the Extra for all Intents/Activities in my app?
Where and how do I actually code to retrieve the Data payload once the app comes to the foreground?
Thanks!
ADDED:
I mean, I alraedy have 10+ Activities, and there will be more when the app is finished. So, do I have to retrieve the Extra for all Activities to see if the app has been re-opened with any Push data payload?
Upvotes: 0
Views: 942
Reputation: 38289
In the document you linked in your question, it states:
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity
The launcher activity is specified in the manifest using category LAUNCHER. For example:
<activity
android:name="com.example.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can override the default behavior to specify another activity. In your message notification data, add property click_action
with value of an action string. Then create the activity and give it an intent filter in the manifest that matches the action. For example, with message:
{
"to": "dhVgCGVkTSR:APA91b...mWsm3t3tl814l",
"notification": {
"title": "New FCM Message",
"body": "Hello World!",
"click_action": "com.example.FCM_NOTIFICATION"
},
"data": {
"score": "123"
}
}
Define the intent filter like this:
<activity android:name=".MyFcmNotificationActivity">
<intent-filter>
<action android:name="com.example.FCM_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And to clarify the documentation a bit, the data payload is not delivered to the activity when the message is received; it's delivered when the user clicks on the notification.
Upvotes: 2
Reputation: 8841
You will have to extend the FirebaseMessagingService
class.
And override the onMessageReceived
method.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification
method below.
}
Make sure you register the service in the manifest.
Upvotes: 0