Here is my manifest
<service android:name=".fcm.PshycoFirebaseMessagingServices">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.PshycoFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
When the app is in background and notification arrives then the default notification comes and doesn't run my code of onMessageReceived
.
Here is my onMessageReceived
code. This invokes if my app is running on foreground, not when app in background. How to run this code when the app is in background too?
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// 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.
data = remoteMessage.getData();
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
String imageUrl = (String) data.get("image");
String action = (String) data.get("action");
Log.i(TAG, "onMessageReceived: title : "+title);
Log.i(TAG, "onMessageReceived: message : "+message);
Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
Log.i(TAG, "onMessageReceived: action : "+action);
if (imageUrl == null) {
sendNotification(title,message,action);
} else {
new BigPictureNotification(this,title,message,imageUrl,action);
}
}
// [END receive_message]
Since the
display-messages
which are sent from Firebase Notification UI only works if your app is in foreground. Fordata-messages
, there is a need to make a POST call to FCMSteps
Install Advanced Rest Client Google Chrome Extension
Add the following headers
Key: Content-Type, Value: application/json
Key: Authorization, Value: key="your server key"
Add the body
If using topics :
If using registration id :
Thats it!. Now listen to
onMessageReceived
callback as usual.According to the docs: May 17, 2017
So,you should use both of the payload notification + data:
There is no need to use click_action.You should just get exras from intent on LAUNCHER activity
Java code should be on onCreate method on MainActivity :
You can test both of the payload notification + data from Firebase Notifications Console . Don't forget to fill custom data fields on Advanced options section
Remove notification payload completely from your server request. Send only data and handle it in
onMessageReceived()
, otherwise youronMessageReceived
will not be triggered when the app is in background or killed.Here is what I am sending from server:
So you can receive your data in
onMessageReceived(RemoteMessage message)
like this: (let's say I have to get the id)And similarly you can get any data which you have sent from server within
onMessageReceived()
.Here is more clear concepts about firebase message. I found it from their support team.
Firebase has three message types:
Notification messages : Notification message works on background or foreground. When app is in background, Notification messages are delivered to the system tray. If the app is in the foreground, messages are handled by
onMessageReceived()
ordidReceiveRemoteNotification
callbacks. These are essentially what is referred to as Display messages.Data messages: On Android platform, data message can work on background and foreground. The data message will be handled by onMessageReceived(). A platform specific note here would be: On Android, the data payload can be retrieved in the Intent used to launch your activity. To elaborate, if you have
"click_action":"launch_Activity_1"
, you can retrieve this intent throughgetIntent()
from onlyActivity_1
.Messages with both notification and data payloads: When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification. When in the foreground, your app receives a message object with both payloads available. Secondly, the
click_action
parameter is often used in notification payload and not in data payload. If used inside data payload, this parameter would be treated as custom key-value pair and therefore you would need to implement custom logic for it to work as intended.Also, I recommend you to use
onMessageReceived
method (see Data message) to extract the data bundle. From your logic, I checked the bundle object and haven't found expected data content. Here is a reference to a similar case which might provide more clarity.For more info visit my this thread
June 2018 Answer -
You have to make sure there is not a "notification" keyword anywhere in the message. Only include "data", and the app will be able to handle the message in onMessageReceived, even if in background or killed.
Using Cloud Functions:
Then in your onMessageReceived(), in your class extending com.google.firebase.messaging.FirebaseMessagingService :
In addition to above answers, If you are testing push notifications using FCM console, 'data' key and object is not added to Push Notification bundle. So you will not receive detailed push notification when App is background or killed.
In this case you have to opt for your back end admin console to test App background scenario.
Here, you will have added 'data' key to your push bundle. so, detailed push will be shown as expected. Hope this helps few.