0

I'm trying to use fcm (firebase cloud messaging) to create web push notification, so I started by creating my project on firebase than I got the API key that I added to the HTTP header request, I used wsClient offered by play to send POST HTTP request to the firebase endpoints, But I got the error 401, I tried to use server key, legacy key but it is always the same result.

def sendNotification()={
var  url = "https://fcm.googleapis.com/fcm/send"
val request: WSRequest = ws.url(url)
  .withHttpHeaders("Authorization"->s" $myserverKey","Content-Type" ->"application/json")
val data = Json.obj()
val futureResponse = request.post(data)
Await.result(futureResponse, Duration.Inf)
Logger.warn(s"send notification to firebase $futureResponse")

error message:

401: The request was missing an Authentication Key (FCM Token). Please, 
refer to section "Authentication" of the FCM documentation, at 
https://firebase.google.com/docs/cloud-messaging/server.
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    See https://stackoverflow.com/questions/45309674/fcm-with-postman-the-request-was-missing-an-authentication-key-fcm-token/45310143. Gut feeling is that you're missing the `key=` prefix in the value. – Frank van Puffelen Oct 26 '18 at 10:18
  • it works, but I got a failure response : "error": "InvalidRegistration" what could be the cause? – mohamedali10 Oct 26 '18 at 10:36
  • That's a different problem, meaning that the token your using is not known to the FCM server. Search Stack Overflow for the error message and you should get some good his. – Frank van Puffelen Oct 26 '18 at 13:59

1 Answers1

0

It looks like you're not following the protocol for putting the server key in the Authorization header. According to the documentation, it should look like this:

Authorization: key=YOUR-KEY
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441