B4J Question Push messages problem

kisoft

Well-Known Member
Licensed User
Longtime User
HI
I follow this tutorial and everything works.
https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

How to implement / call the "SendMessage" code within a UI (desktop) B4J Application ?
I do it this way, but unfortunately it does not send PUSH messages

B4X:
#Region Project Attributes
    #MainFormWidth: 1000
    #MainFormHeight: 600
    #CommandLineArgs:
    #MergeLibraries: true
#End Region


Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private const API_KEY As String ="my key..........."
    Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1")
    MainForm.Show
 End Sub

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)
    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")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
    
End Sub


Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
    
End Sub

Sub Button1_Click

    SendMessage("general","TEST","Message")

End Sub

After clicking on the button nothing happens. Any suggestions?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Make sure to select jOkHttpUtils2 in the libraries tab and not jOkHttpUtils2_NONUI.

Note that you can simplify your code:
B4X:
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json")
    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
 
Upvote 0
Top