B4R Code Snippet Firebase Push Messages (FCM)

Discussion in 'B4R Code Snippets' started by Erel, Jun 25, 2017.

Similar threads

B4A Code Snippet [B4X] Convert bitmap to BMP format
B4R Tutorial SD cards
B4A Code Snippet [B4X] Bytes To File
B4R Tutorial ESP8266 + UDP + BMP180 = Simple weather station
B4R Tutorial Arduino + Raspberry Pi
  1. Erel

    Erel Administrator Staff Member Licensed User

    This code uses rHttpUtils2 to send push messages. It is equivalent to the B4J/A/i code posted here: https://www.b4x.com/android/forum/t...s-firebase-cloud-messaging-fcm.67716/#content

    Notes:
    - #StackBufferSize should be set to 500+.
    - The title and body messages are not escaped. You will need to manually escape the messages if they include quotes.

    Code:
    #Region Project Attributes
       
    #AutoFlushLogs: True
       
    #CheckArrayBounds: True
       
    #StackBufferSize: 600
    #End Region

    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private API_KEY() As Byte = "AAAAOkXaaasasasasaaaaaaaaaaaaaaaaaaasas2d3d23"
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       
    'send the android message
       SendMessage("general""This is the title""This is the body")
    End Sub

    Sub JobDone (jr As JobResult)
       
    Log("JobDone: ", jr.JobName)
       
    Log("Success: ", jr.Success)
       
    If jr.JobName = "ios_general" Then
         
    'send the ios message
         SendMessage("ios_general""This is the title""This is the body")
       
    End If
    End Sub

    Private Sub SendMessage(Topic() As Byte, Title() As Byte, Body() As Byte)
       
    Dim bc As ByteConverter
       
    HttpJob.Initialize(bc.StringFromBytes(Topic))
       
    Dim buffer(300As Byte 'must be large enough to hold the message payload
       Dim raf As RandomAccessFile
       raf.Initialize(buffer, 
    True)
       WriteBytes(raf, 
    "{""data"":{""title"":""")
       WriteBytes(raf, Title)
       WriteBytes(raf, 
    """,""body"":""")
       WriteBytes(raf, Body)
       WriteBytes(raf, 
    """}")
       
    'end of data
       WriteBytes(raf, ",""to"":""\/topics\/")
       WriteBytes(raf, Topic)
       WriteBytes(raf, 
    """")
       WriteBytes(raf, 
    ",""priority"": 10")
       
    If bc.StartsWith(Topic, "ios_"Then
         WriteBytes(raf, 
    ",""notification"": {""title"": """)
         WriteBytes(raf, Title)
         WriteBytes(raf, 
    """,""body"":""")
         WriteBytes(raf, Body)
         WriteBytes(raf, 
    """, ""sound"": ""default""}")
       
    End If
       WriteBytes(raf, 
    "}")
       
    HttpJob.AddHeader("Authorization", JoinBytes(Array("key=".GetBytes, API_KEY)))
       
    HttpJob.AddHeader("Content-Type""application/json")
       
    Log("stack: ", StackBufferUsage, ", buffer size:", raf.CurrentPosition)
       
    HttpJob.Post("https://fcm.googleapis.com/fcm/send", bc.SubString2(buffer, 0, raf.CurrentPosition))
    End Sub

    Private Sub WriteBytes(raf As RandomAccessFile, Data() As Byte)
       raf.WriteBytes(Data, 
    0, Data.Length, raf.CurrentPosition)
    End Sub
    Depends on rHttpUtils2 module (add it with Project - Add existing module): https://www.b4x.com/android/forum/threads/module-rhttputils2-http-client.74785/#content
    Also depends on: rESP8266WiFi and rRandomAccessFile.
    It should be simple to convert it to use rEthernet instead of rESP8266WiFi.
     
    Peter Simpson likes this.
  2. derez

    derez Expert Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

    This one is better.
    It is based on rHttpUtils2 so the code is simpler, it supports modifying the "body" message and it behaves like the B4J code (sending the message as an iOS or Android message based on the topic).
     
    Peter Simpson likes this.
  4. derez

    derez Expert Licensed User

    The part for wifi connection is missing from the code above in appstart :
    Code:
    If wifi.Connect("link"Then
            
    Log("Connected to router")
        
    Else
            
    Log("Failed to connect")
            
    Return
        
    End If
     
    Erel and XorAndOr like this.
  5. rbghongade

    rbghongade Active Member Licensed User

    Dear friends,
    How do we use FCM with a GSM/GPRS module like SIM800?
    regards,
     
  6. Erel

    Erel Administrator Staff Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice