I\'m working with Firebase and testing sending notifications to my app from my server while the app is in the background. The notification is sent successfully, it even appe
If app is in the background mode or inactive(killed), and you click on Notification, you should check for the payload in LaunchScreen(in my case launch screen is MainActivity.java).
So in MainActivity.java on onCreate check for Extras:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d("MainActivity: ", "Key: " + key + " Value: " + value);
}
}
The backend I'm working with is using Notification messages and not Data messages. So after reading all the answers I tried to retrieve the extras from the bundle of the intent that comes to the launched activity.
But no matter which keys I tried to retrieve from getIntent().getExtras();
, the value was always null.
However, I finally found a way to send data using Notification messages and retrieve it from the intent.
The key here is to add the data payload to the Notification message.
Example:
{
"data": {
"message": "message_body",
"title": "message_title"
},
"notification": {
"body": "test body",
"title": "test title"
},
"to": "E4An.."
}
After you do this, you will be able to get your info in this way:
intent.getExtras().getString("title")
will be message_title
and
intent.getExtras().getString("message")
will be message_body
Reference
This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.
You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user. The main activity is used if no click_action is specified.
When the intent is launched you can use the
getIntent().getExtras();
to retrieve a Set that would include any data sent along with the notification message.
For more on notification message see docs.
Just override the OnCreate method of FirebaseMessagingService. It is called when your app is in background:
public override void OnCreate()
{
// your code
base.OnCreate();
}
The point which deserves highlighting is that you have to use data message - data key only - to get onMessageReceived handler called even when the app is in background. You shouldn't have any other notification message key in your payload, otherwise the handler won't get triggered if the app is in background.
It is mentioned (but not so emphasized in FCM documentation) here:
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.
I had the same problem. It is easier to use the 'data message' instead of the 'notification'. The data message always load the class onMessageReceived.
In that class you can make your own notification with the notificationbuilder.
Example:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}
private void sendNotification(String messageTitle,String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500,500,500,500,500};
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}