B4J Question Problem Calling Sub

Isac

Active Member
Licensed User
Longtime User
I can not recall the sub from a Button
somebody can give me a hand

How can I pass the function variables?

B4X:
#Region Project Attributes 
    #MainFormWidth: 600
    #MainFormHeight: 400 
#End Region



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

End Sub

   
Sub AppStart (Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    SendMessage("all","test", "test!!!!")

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("idnews":1)
    Dim notification 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)
    Log(m)
    Log(data)
   
End Sub       




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



Sub Button1_MouseClicked (EventData As MouseEvent)

SendMessage(Topic,Title,Body)


End Sub
 

stevel05

Expert
Licensed User
Longtime User
You either need to pass the values as String Literals as you have in appStart, or you need to create Global Variables, update the values and pass those.

It depends on what you want to do at that point in the program.
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
I tried to recall the function in this way, the message is built but not sent.


B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)

SendMessage("all","Title","Body")

End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I tried to recall the function in this way, the message is built but not sent.
Change
B4X:
Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
End Sub
to
B4X:
Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    Else
        Log($"Something went wrong: ${job.ErrorMessage}"$)
    End If
    job.Release
End Sub
and see what error message you get.
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
job is not recalled,

if I call it this way it works but it is not sent


B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)
   
Dim Job As HttpJob
SendMessage("all","TITLE","BODY")
JobDone(Job)


End Sub



[jobname=, success=false, username=
, password=, errormessage=, target=null
, taskid=, req=null, tag=null
, httputils2service=null]
Something went wrong:
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Try this for SendMessage (switched to using Wait For). Take/comment out job related stuff in MouseClicked event
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("", Me)  '<---- Changed
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("idnews":1)
    Dim notification 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)
    Log(m)
    Log(data)
    ' Use Wait For
    Wait For (job) JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    Else
        Log($"Something went wrong: ${job.ErrorMessage}"$)
    EndIf
    job.Release
End Sub
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
Undeclared variable wait........


B4J version: 4.70
Parsing code. Error
Error parsing program.
Error description: Missing Keyword: next
Occurred on line: 48 (Main)
End Sub


B4X:
    ' Use Wait For
    Wait For (Job) JobDone(Job As HttpJob)
    Log(Job)
    If Job.Success Then
        Log(Job.GetString)
    Else
        Log($"Something went wrong: ${Job.ErrorMessage}"$)
    End If
    Job.Release
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Download the latest version of B4j
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Just a note (from an old Erel's post): Resumable subs is a new feature added in B4J v5.50 / B4i v4.00 / B4A v7.00.
In post #7 above you show the use of B4J 4.70 so the missing "Next" is relative to the For in Wait For, I presume, since that features was not yet available on that B4J release.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…