Android Question Send firebase notification through B4A

Mattiaf

Active Member
Licensed User
Hi guys, is it possible to send a firebase notification through b4a or it's only possible through b4j?
Thanks
 
Solution
I found the answer by myself.
Yes - that's possible.
Using the libs: httpUtils2 and Json

B4X:
Private const API_KEY As String = "your api key"
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
    End If
    m.Put("priority", 10)
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)...

Mattiaf

Active Member
Licensed User
I found the answer by myself.
Yes - that's possible.
Using the libs: httpUtils2 and Json

B4X:
Private const API_KEY As String = "your api key"
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
    End If
    m.Put("priority", 10)
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
    Wait For (Job) JobDone(Job As HttpJob)
    Log(Job)
    If Job.Success Then
        Log(Job.GetString)
    End If
    Job.Release
End Sub

B4X:
Private Sub Button1_Click
    SendMessage("general", "title", "body")
End Sub
 
Upvote 0
Solution
Top