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]
1. Why is this happening?
There are two types of messages in FCM (Firebase Cloud Messaging):
onMessageReceived()
callback only when your app is in foregroundonMessageReceived()
callback even if your app is in foreground/background/killedFirebase team have not developed a UI to send
data-messages
to your devices, yet.2. How to?
To achieve this, you have to perform a
POST
request to the following URL:Headers
Content-Type
, Value:application/json
Authorization
, Value:key=<your-server-key>
Body using topics
Or if you want to send it to specific devices
3. How to handle the push notification message?
This is how you handle the received message:
I feel like all the responses are incomplete but all of them have something that you need to process a notification that have data when your app is in background.
Follow these steps and you will be able to process your notifications when your app is in background.
1.Add an intent-filter like this:
to an activity that you want to process the notification data.
Send notifications with the next format:
The key here is add
where .MainActivity is the activity with the intent-filter that you added in step 1.
Get "data" info from notification in the onCreate of ".MainActivity":
And that should be all you need to do. I hope this helps somebody :)
is not called every time it is called only when app is in forground
there is one override method this method is called every time , no matter what app is in foreground or in background or killed but this method is available with this firebase api version
this is the version u have to import from gradle
this is the method
with previous firebase api this method was not there so in that case fire base handle itself when app is in background .... now u have this method what ever u want to do ... u can do it here in this method .....
if you are using previous version than default activity will start in that case u can get data same way
// do what ever u want .... }
generally this is the structure from server we get in notification
it's up to u how u want to give that data key or u want give notification anything u can give ....... what ever u will give here with same key u will get that data .........
there are few cases if u r not sending click action in that case when u will click on notification default activity will open , but if u want to open your specific activity when app is in background u can call your activity from this on handleIntent method because this is called every time
I figured out the scenarios,
When app is in foreground, onMessageReceived() method is called from the FirebaseService.So the pendingIntent defined in the service class will be called.
And when app is in background, first activity is called.
Now, if you use a splash activity, then must keep in mind the splashactivity will be called, else if there is no splashActivity, then whatever the first activity is, will be called.
Then you need to check getIntent() of the firstActivity to see if it has any bundle .if everything is alright you will see bundle is there with values filled in. If the value in data tag sent from server looks like this,
Then in the first activity, you will see, there is a valid intent( getIntent() is not null) , valid bundle and inside bundle , there will the whole JSON mentioned above with data as key.
For this scenario, code for extraction of value will look like this,
To make firebase library to call your onMessageReceived() in the following cases
you must not put JSON key 'notification' in your request to firebase API but instead use 'data', see below.
The following message will not call your onMessageReceived() when your app is in the background or killed, and you can't customize your notification.
but instead using this will work
Basically, the message is sent in the argument RemoteMessage along with your data object as Map, then you can manage the notification in onMessageReceived as in the snippet here
According to the firebase documentation in send downstream using firebase, there is 2 type of payload :
data
notification
When you are in the foreground you can get the data inside FCM using onMessageReceived(), you can get your data from data payload.
When you are in background, FCM will showing notification in system tray based on the info from notification payload. Title, message, and icon which used for the notification on system tray are get from the notification payload.
This notification payload are used when you want to automactically showing notification on the system tray when your app is in the background. To get notification data when your app in the background, you should add click_action inside notification payload.
Put that intent-filter on your manifest, inside application tag. When you click the notification, it will open the app and go straight to activity that you define in click_action, in this case "OPEN_ACTIVTY_1". And inside that activity you can get the data by :
I'm using FCM for my android app and use both of the payload. Here is the example JSON i'm using :