B4R Question [solved] FCM Forbidden response

josejad

Expert
Licensed User
Longtime User
Hi all:

Since a couple of months maybe, I've stopped receiving FCM Notifications from my ESP8266

I got it following this thread.
https://www.b4x.com/android/forum/threads/firebase-push-messages-fcm.81025/

Now, I've connected my ESP8266 to my PC again to see the errors, and I get:
B4X:
.............
********************* INICIANDO PROGRAMA ****************
Ȥl���X�$�x$:I��AppStart
Conectado a red WIFI
Connected to broker
Connected to server FCM
SendAndroid
HTTP/1.1 403 Forbidden
Content-Type: text/html; charset=UTF-8
Date: Tue, 26 Nov 2019 08:37:55 GMT

Expires: Tue, 26 Nov 2019 08:37:55 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: 
nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Accept-Ranges: n
one
Vary: Accept-Encoding
Transfer-Encoding: chunked
8d
<HTML>
<HEAD>
<TITLE>Forbidden</TITLE>

And after a while, I get a time out
B4X:
HTTP/1.1 408 Request Timeout

I've been searching and I've found some people get the same error, and it seems to be related with http, now needs to be https
https://stackoverflow.com/questions/57961829/legacy-fcm-response-now-forbidden

But I've been reading the doc
https://firebase.google.com/docs/cloud-messaging/send-message?hl=en

and it seems to be rigth the HTTP/1.1
B4X:
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

{
   "message":{
      "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
      "notification":{
        "body":"This is an FCM notification message!",
        "title":"FCM Message"
      }
   }
}

Nobody is having this issue?

Thanks in advance
 

josejad

Expert
Licensed User
Longtime User
Thanks Erel:

Hum... it seems I was not using the link I posted for FCM messages, but this one, with the legacy API
B4X:
Private const API_KEY As String = "jpiadjfpadijPJIPOijpeijpe"

Sub ConnectFCM(unused As Byte)
    If socket.ConnectHost("fcm.googleapis.com", 80) = False Then
        Log("trying to connect again to FCM")
        CallSubPlus("ConnectFCM", 1000, 0)
        Return
    End If
    Log("Connected to server FCM")
    astream.Initialize(socket.Stream, "Astream_NewData", "Astream_Error")
    For i = 1 To 2
        astream.Write("POST /fcm/send HTTP/1.1").Write(eol)
        astream.Write("Host: fcm.googleapis.com").Write(eol)
        astream.Write("Authorization: key=").Write(API_KEY).Write(eol)
        astream.Write("Content-Type: application/json").Write(eol)
        If i = 1 Then SendAndroid Else SendIOS
    Next
End Sub

I've updated my code with the code in the link, and the server api and now it's working.
 
Upvote 0
Top