FCM when app in background
Hi,
The FCM works fine when the app is in foreground and a push comes, but when the app is in background and the system tray notification is clicked , it just opens up the app without passing any intent extras. I have added the click_action in Notification Payload from Advanced option of Firebase Console to open an activity but it dosen't work. I have also added the intent-filter ( handle backgound app section) as in documentation.
How can i open a specific activity using FCM service along with the data i pass when the app is in background or exit state.
Hi hablema, thanks for giving FCM a try. You mentioned a couple of things:
- you are not receiving the data sent along with the notification message.
- and the click_action from the console does not result in the correct activity being launched.
So if I understand your issues, there are a couple of things to note:
- The data sent along with the notification message (which is the type of notification sent from the console) is accessed from the intent extras of the activity that is launched when the notification is tapped. Have you tried to get the data from the activity intent extra?
- You cannot currently set the click_action field from the console. The custom data in the console are only for data key value pairs, you cannot configure any of the notification message fields via the custom data fields.
Hi @kroikie
I have a follow up question.
According to this documentation, the notification will be delivered to the device's system tray when the app is in background which means onMessageReceived() won't get called, but I wonder if there's any way that I can customize the icon in the notification view. Currently, it shows default white icon.
Hi @kroikie
I am looking for the same solution as mentioned by @wassupnari. Can we achieve that or it is not possible with FCM. As using FCM on iOS we have background mode do Android also have same background mode using FCM or not.
@avirepo @wassupnari these customizations are possible through FCM but not yet through the console UI, you have to use the HTTP API.
You can see more discussion on #4, I am going to close this issue as we are tracking this feature request elsewhere. We know this is important to everyone so the team is working hard on it.
@kroikie : Thanks for info. I got it working as you have mentioned from the intent of the activity. The documentation should have been more clear in that aspect with a full sample manifest. Its good now, also i had a problem in which the FCM messages from console only started delivering after i tried from the HTTP API. Don't know why but after that its working directly from the console. Thanks again.
Hi Team, Thanks for this module.
Works just fine except this issue. when the app is in background , and during cold launch from notification, the app is getting opened but how to access the notification data, and do some activities with that. Unlike the PushNotificationIOS's popInitialNotification, I dont think we have any function that will get the data. Can someone help me if you have made it work. @hablema Thanks in advance
Hi @rimzici, on Android when your application is in the background and a notification message is sent the accompanying data payload (if included) is available via the launched intent resulting from the developer tapping on the notification. By default that would be your MainActivity, you can find an example of this in the messaging sample in this repo.
For more on handling data accompanying a notification message see the docs.
@kroikie , Thank You very much for your response. Actually it worked for me by using FCM.initialData, which provides data, both during cold launch and background running. in the documentation its specified under "Behaviour when sending notification and data payload through GCM" , but since iam using FCM to send push, i must say both users can retrive data using that property.(this.initialData = FCM.initialData;) Thankyou.
So does it mean that data-message only are not received anymore when the application is in background?? The doc is really fuzzy on it.
If yes, that's a very new behavior and it's what I'm facing in my application for few days because I built and published an application on 07/08/16 (FCM 9.2.0) which perfectly receives data-message sent by API while it's in background, on a Samsung GT-9295 5.0.1 and on a Nexus 7 6.0.1 and it's what I do for a long time in GCM then in FCM.
But starting from the working project built one week ago, I realize that now I rebuild it, the generated app is no more able to receive anything in background.
I tried to upgrade to FCM 9.2.1, same.
So there is something new in my environment, sdk, studio, java, I don't know, which causes this very important feature to not work anymore.
Why?
After a deep search, I have found what is going on: There are two types of messages in FCM:
- display-messages: These messages only work when your app is in foreground
- data-messages: Theses messages work even if your app is in background
Firebase team have not developed a UI to send data-messages to your devices.
To achieve this, you have to do a POST to the following URL:
https://fcm.googleapis.com/fcm/send
And the following headers:
- Key:
Content-Type, Value:application/json - Key:
Authorization, Value:key=<your-server-key>
Body:
{
"data": {
"my_custom_key" : "my_custom_value",
"other_key" : true
},
"registration_ids": ["your-device-token","your-device2-token","your-device3-token"]
}
To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key
Hope this helps!
Ref: How to handle notification when app in background in firebase
@pratikbutani That's sure, but according to lot of tests I did, applications built with Studio 2.1.2 can't work.
https://github.com/firebase/quickstart-android/issues/89
Anybody please let me know is it not possible to show custom notification instead of default when app is in background or is not running while using FCM.
@anujjpandey11 Yes it's possible by sending a data message which will be received by onMessageReceive().
Then based on the information sent, you can create your own notification using notification builder.
I already tried it but it is only work when app is in foreground.
Note: I'm sending message from firebase console.
AFAIK, firebase console is not able to send data only messages, unles it has been updated very recently.
To do that, I use Google Advanced REST Client
And configure it like here Of course data are mine, replace everything in data by the infos you need.
Thanks @2ndGAB
Hi Team, @kroikie : Well the issue still persits, I thought i had it solved but i was stupidly mistaken. I looked into the links that you have suggested https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MainActivity.java#L37 , from there can you suggest me some way , how to pass the data from the MainApplication.java(MainActivity.java : RN < 29) to our react-native screens.(say like i need the data on one of my navigator screen). Well after following some others issues and suggestion I tried like way too. Initially, as suggested in react-native-fcm docs, in my backend i was sending data like this, { "to":"some_device_token", "content_available": true, "notification": { "title": "hello", "body": "yo", "click_action": "OPEN_ACTIVITY_1" }, "data": { "extra":"juice" } }
and in my mainfest file, i have
<intent-filter> <action android:name="OPEN_ACTIVITY_1" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
so when app is fully closed and in background on clicking notification my app is lauched, but during background case no data in "FCM.initialData" or "FCM.on('notification', (notif) => { })" event.
with the same manifest I tried DATA ONLY push like
{ "to" : my_token, "data": { "id": 19, "title": "Title test", "msg": "Text of the test" } "content_available": true }
that time notification not even received in the tray. gets data when app in open. So I assume without "notification" key device wont receive push in tray.
So after all I come to conclusion that only way is what you have said already to handle it somehow like
if (getIntent().getExtras() != null) { for (String key : getIntent().getExtras().keySet()) { String value = getIntent().getExtras().getString(key); Log.d(TAG, "Key: " + key + " Value: " + value); } }
someone help me how to pass this data from MainApplication.java to our javascript file. or is there any work around y using react-native-system-notifivation along with our fcm, as they gave done in GCM ???? Need Help Thanks In Advance
Hi. Do anyone have a working solution for this?? I tried by sending "DATA ONLY" message iam able to receive push message inside FCM.on('notification',()=> { }); but not in notification tray, so I have created a custom notification using react-native-system-notification. that has only made the situation worse. Iam not able to receive push notification at "FCM. initialData" , where it used when app is not running. not only that , that module "react-native-system-notification" is not maintained well , it has alot issues. So got to stick with "notification" message or hybrid message. someone help how to receive notification data when app is in background.
@rimzici I don't think that, messages from the Firebase console (notification messages) are supported by React Native. I would suggest that you use data messages (sent from the REST API) and generate your own notifications.
Hi, @kroikie , Well, may be i would have messed up with something, still a newbie. Anyway finally It all came together. after upgrading to "react": "^15.2.1", "react-native": "^0.31.0", "react-native-fcm": "^1.0.16",
it all works perfectly. thank you very much team.