3

From the document on Firebase website: https://firebase.google.com/docs/cloud-messaging/downstream

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?

UPDATE:

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

diousk
  • 634
  • 1
  • 8
  • 13

2 Answers2

7

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

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Hi, can this be applied to Firebase Notifications without server side code? If so, can you provide some code to achieve it on Android Studio, thank you so much. – Minion May 24 '16 at 19:59
  • push messages can only be sent from the server – tyczj May 24 '16 at 20:29
  • So, there is no option to receive the notification into the app if it is in the background (from Firebase Notifications only), right? Is there any way to at least change the icon? Currently it auto-uses the app icon. – Minion May 24 '16 at 20:42
  • I dont understand what you mean you can get push messages at any point of the app life-cycle (foreground/background) – tyczj May 24 '16 at 20:47
  • According to the [docs](https://firebase.google.com/docs/notifications/android/console-audience#receive_and_handle_notifications) if the app is in the background the notification will be sent to the system tray _Notification: system tray_, _Data: in extras of the intent._, so it means that I can't handle notifications if the app is in the background, the problem is that my app has an icon that is a circle and it takes it in white, but on the foreground notification, I change it to a particular transparent icon, so is there any way to change the icon if the app is in the background? – Minion May 25 '16 at 13:57
  • dont use the notification tag in your payload like I said in my answer – tyczj May 25 '16 at 13:59
  • you saved my time dear! @tyczj – Ahmad Vatani Jul 11 '16 at 11:29
4

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

Yes, don't send the push message from Firebase Console, send it using the API curl request.

HTTP POST Request

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

Above push will be received in onMessageReceived() method even if your app is in background or killed or running.

Hisham Muneer
  • 8,558
  • 10
  • 54
  • 79