B4R Question Can't send FirebaseNotifications with Arduino Ethernet anymore.

Åke Johansson Bravida

Member
Licensed User
Can't send FirebaseNotifications with Arduino Ethernet anymore.

I can't send firebase push. I get error from google server.

Error Log:
********************* PROGRAM STARTING ****************
AppStart
Connected to network. My ip address: 192.168.1.107
Connected to server
HTTP/1.1 403 Forbidden
Content-Type: text/html; charset=UTF-8
Date: Thu, 05 Dec 2019 13:29:49 GMT
Expires: Thu, 05 Dec 2019 13:29:49 GMT


This is working code but for some reason it's not working anymore.
Do I need to use https ?

Can't find any topic for this problem.
Can someone help me to solve this problem?

Thanks for considering my request.


B4X:
' 2019-08-11
#Region Project Attributes
 #AutoFlushLogs: True
 #CheckArrayBounds: True
 #StackBufferSize: 250
#End Region
Sub Process_Globals
 'These global variables will be declared once when the application starts.
 'Public variables can be accessed from all modules.
 Private Sent As Boolean = False
 Public BtnState As Boolean = True
 
 'Public SendPushIntervall As Timer
 Public Serial1 As Serial
 Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
 Private btn As Pin
 Private eth As Ethernet
 Private astream As AsyncStreams
 Private socket As EthernetSocket
 Private storedMessage(30) As Byte 'title allocate 30 bytes for the message
 Private storedMessage1(30) As Byte 'body allocate 30 bytes for the message1
 Private bc As ByteConverter
 Private const API_KEY As String = "AAAAmUeURI"
 Private eol() As Byte = Array As Byte(13, 10)
End Sub
Private Sub AppStart
 Serial1.Initialize(115200)
 Log("AppStart")
 
 If eth.InitializeDHCP(MacAddress) = False Then 'change as needed
  Log("Error connecting to network.")
  Return
 Else
  Log("Connected to network. My ip address: ", eth.LocalIp)
 End If
 
 btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP)
 btn.AddListener("btn_StateChanged")
End Sub
Sub Btn_StateChanged (State As Boolean)
 'Log("Btn_StateChanged")
 If State = False And Sent = False Then
  BtnState = False
  If Sent = False Then
    'LarmDelay.Enabled = True
    SendMessage("Willo Maskin problem Kyltork !!", "FEL! Kyltorken  L A R M A R  !!!")
    'Log("Larm ingång fördröjning aktiverad!")
    Sent = True
    'Log("Larm 1 minuts fördröjning aktiverad!")
    
   End If
   End If
 If State = True Then
   BtnState = True
   If Sent = True Then
    Sent = False
   Log("Larm ingång fördröjning avstängd!")
   
   End If
 End If
End Sub

Sub SendMessage(str() As Byte, str1() As Byte)
 bc.ArrayCopy(str, storedMessage)
 bc.ArrayCopy(str1, storedMessage1)
 Connect(0)
End Sub
Sub Connect(unused As Byte)
 If socket.ConnectHost("fcm.googleapis.com", 80) = False Then
  Log("trying to connect again")
  CallSubPlus("Connect", 1000, 0)
  Return
 End If
 Log("Connected to server")
 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("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
Sub SendAndroid
 astream.Write("Connection: keep-alive").Write(eol) '<--- keep-alive
 'astream.Write("Content-Length: ").Write(NumberFormat(56 + storedMessage.Length, 0, 0)).Write(eol)
 astream.Write("Content-Length: ").Write(NumberFormat(56 + storedMessage.Length + storedMessage1.Length, 0, 0)  ).Write(eol)
 astream.Write(eol)
 'astream.Write("{""data"":{""title"":""").Write(storedMessage).Write(""",""body"":""""},""to"":""\/topics\/general""}")
    astream.Write("{""data"":{""title"":""").Write(storedMessage).Write(""",""body"":""").Write(storedMessage1).Write("""},""to"":""\/topics\/general""}")
End Sub
Sub SendIOS
 astream.Write("Connection: close").Write(eol) '<--- close
 astream.Write("Content-Length: ").Write(NumberFormat(136 + 2 * storedMessage.Length, 0, 0)).Write(eol)
 astream.Write(eol)
 astream.Write("{""notification"":{""sound"":""default"",""4"":""") _
  .Write(storedMessage) _
  .Write(""",""body"":""B4R""},""data"":{""title"":""").Write(storedMessage).Write(""",""body"":""B4R""}") _
  .Write(",""to"":""\/topics\/ios_general"",""priority"":10}")
End Sub
Sub Astream_NewData (Buffer() As Byte)
   Log(Buffer)
End Sub
Sub Astream_Error
   Log("Disconnected")
End Sub

code
 

Åke Johansson Bravida

Member
Licensed User
Yes I have read that but I realy dont understand what he did to solve the problem?
I tryed with this API_KEY in B4J and that is ok.
I have not an old API_KEY.

Erel Wrote
Maybe you are using an old version of the API_KEY.

Should I generate a new API_KEY?
 
Upvote 0

Åke Johansson Bravida

Member
Licensed User
Ok I created a new API_KEY but I still got the same problem.
B4R code is not working any more.
B4J is still working but only for my test purposes. I dont like to run this on a PC.:(
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi:

Try with the code Erel link in the above post. I was using another code (from another post I can't find right now), but the code above, worked for me.
 
Upvote 0

Åke Johansson Bravida

Member
Licensed User
Upvote 0
Top