0

After discovering that sending notifiactions from the Firebase console, if the app is in background the onMessageReceived() method is not called.

I therefore followed peoples advice to use a HTTP POST Request. I have been using postman to make a post request

https://fcm.googleapis.com/fcm/send

with 2 headers:

Content-Type: application/json

Authorization: key=ALsa.......6ryLiI

and body

 {
 "to" : "fzgh-aHiOd8:APA91bFcKuwuVq_2gbLydHkAZf9XLUzm9vPalh7EUQT3Kyj8mcW9raAs9QvxbHaGJ4-U4RnUJwJ3UaEarbMXiQ8rHnsx9UuYBDrOI9tJYB78_Z3VVR1l7H_6PGpCkdgINx8a4vvMXD9q",
"notification" : {
    "body" : "Sales!",
    "title" : "ID Phone",
    "icon" : "myicon"
},
"data" : {
    "data" : "sales",

}

}

Now, the notification does come into my app (which is good), but its still not the custom notifcation that I wanted. ie onMessageReceived() is still not being called.

Is there something I am missing?

RJB
  • 1,704
  • 23
  • 50
  • check whether data is recieved or not. as your registrant id or server api key might not be correct – Shubham AgaRwal Jul 23 '16 at 19:28
  • Data is being received. Still not working though. Thanks – RJB Jul 23 '16 at 20:14
  • When you send a notification message and your app is in the background, a notification will be displayed in the notification tray. Your app's `onMessageReceived` will not be invoked. This is expected behavior. See http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase – Frank van Puffelen Jul 23 '16 at 23:18

1 Answers1

3

You should define a data payload to get a message in the onmessagereceived(). notification tag is for notifications only.

See this from GCM documentation:

Data messages

Set data with your custom key/value pairs to send a data payload to the client app. Data messages can have a maximum 4KB payload.

For example, here is a JSON-formatted message in the same IM application as above, where the information is encapsulated in data and the client app is expected to interpret the content:

 {
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

EDIT

Replace these

Content-Type - application/json
Authorization - key=ALsa.......6ryLiI

with

Content-Type:application/json
Authorization:key=ALsa.......6ryLiI
ugur
  • 3,604
  • 3
  • 26
  • 57