diousk diousk - 7 months ago 593
Android Question

Can firebase cloud messages be detected by app in background?

From the document on Firebase website:

Notifications delivered when your app is in the background. In this case, the notification is delivered to the device's system tray. A user click on a notification opens the app launcher by default.

If I need to do something when my app receives cloud message and my app is in background.

Is it possible to receive notification in custom BroadcastReceiver or Service class instead of delivering to device's system tray?

Is the only way to set "click_action" as described in https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
when user clicks the notification so that my app is able to be aware of that there's a cloud message arrived?


As mentioned by tyczj, the following curl command example did this work:

curl --header "Authorization: key=$server_key" --header Content-Type:"application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\": \"/topics/friendly_engage\",\"data\": {\"score\":\"123\"}}"

and the message will appear on the callack "onMessageReceived" in the Service class which extends from FirebaseMessagingService


if you want to get the push in your app and not have a notification appear you should remove the notification tag from your push payload and replace it with a data tag

read more about how your payload works here https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages