B4J Question push works but App close (solved)

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi guys,

I have been using the push B4J-App to send push notifications.
but now we want to have a small b4J-App then the manager of the cafes/restaurants can send those pushs.

I have this first basic demo working, based in the original example.
It works fine using this code: Edit: The B4J-App sent the push but immediately after that the b4j-app is closed.
B4X:
     For i = 1626 To 1626 'test my Smartphone A30 number 1626
        SendMessage(i, "From Cafe", TextArea1.text)
        Log("Android: "& i & " - push sent " & TextArea1.text)
    Next

if I use the code below, it also works, I got the push in my Smartphone when I write 1626 in the text boxes (picture attached).
Just the problem is:
The B4J-App sent the push but immediately after that the b4j-app is closed.
B4X:
    Dim i As Int
    Dim a, b As String 'Int

    a = TextFieldStart.text
    b = TextFieldFinish.text

    For i = a To b 'test my Smartphone A30 number 1626
        SendMessage(i, "From Cafe", TextArea1.text)
        Log("Android: "& i & " - push sent " & TextArea1.text)
    Next

the logs:
B4X:
Android: 1626 - push sent test push
[jobname=fcm, success=true, username=
, password=, errormessage=, target=class b4j.example.lkk.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@115f7e6f, response=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@16933101
, tag=java.lang.Object@68edf243, fx=anywheresoftware.b4j.objects.JFX@6d5037db, main=null
, httputils2service=null]
{"message_id":4204494957695715611}


Full code:
B4X:
#Region  Project Attributes
    #MainFormWidth: 900
    #MainFormHeight: 700
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Pane1 As Pane
    Private TextArea1 As TextArea
    'IOS
    Private const API_KEY As String = ""????????????????""
    'Android
    Private const API_KEY As String = "????????????????"

    Private Button2Android As Button
    Private Button1iPhone As Button
    Private TextFieldStart As TextField
    Private TextFieldFinish As TextField
End Sub

Sub AppStart (Form1 As Form, Args() As String)

    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("Main") 'Main layout file
    Pane1.LoadLayout("InnerLayout")
    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)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
        m.Put("priority", 10)
    End If
    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)
End Sub


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

Sub Button2Android_Click
    '************ test FOR ***********
    'Android
    Dim i As Int
    Dim a, b As String 'Int

    a = TextFieldStart.text
    b = TextFieldFinish.text

    'For i = 1626 To 1626 'test my Smartphone A30 number 1626
    For i = a To b 'test my Smartphone A30 number 1626
        SendMessage(i, "From Cafe", TextArea1.text)
        Log("Android: "& i & " - push sent " & TextArea1.text)
    Next

End Sub

Sub Button1iPhone_Click
    '************ test FOR ***********
    'IOS
    Dim i As Int
    For i = 768 To 768 'my iPhone 5 number 768
        SendMessage("ios_"&i, "From Cafe", TextArea1.text)
        Log("iPhone: " & i & " - push sent" & TextArea1.text)
    Next
End Sub

Untitled.jpg


thanks guys.
 
Last edited:

f0raster0

Well-Known Member
Licensed User
Longtime User
B4X:
Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
    'ExitApplication '!  '<---- solved
End Sub
 
Upvote 0
Top