Android Question Problem 'Undeclared variables' in FCM example

JohnnyHamburg

Member
Licensed User
Longtime User
Hello,
I tried to get the FCM working, following Erel´s and KMatel's tutorials.
I think I did everything correct.
Registered to firebase
got the google-services.json file
put the manifest snippets to manifest
put the code to the Editor and inserted my Server-key

the "main"-code:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Process_Globals
   Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n.Notify(1)
End Sub

Sub Service_Destroy

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;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & "AA...................")
End Sub


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

the "Starter"-code:

B4X:
Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
'Starter service
Sub Process_Globals

End Sub

Sub Service_Create
   CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub

Now it tells me this:
Undeclared variable 'firebasemessaging' is used before it was assigned any value
Undeclared variable 'main' is used before it was assigned any value

and I can not compile and go further:

job4afcm2.JPG



Thank you for help.
 

JohnnyHamburg

Member
Licensed User
Longtime User
Sorry, but I don`t want to upload my project, because it has my Server-Key and evrerything else in it, so anybody could use it.
Am I right?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I agree. Serverkey in your code should not uploaded here.

BUT: without seeing your project we just can GUESS (I HATE guessing; so don´t expect more answers from me then)

Strip it down to a small example app which shows the problem.. And upload the sample-project then
 
Upvote 0

JohnnyHamburg

Member
Licensed User
Longtime User
Ok Manfred, Danke für deine Hilfe.


Then I'll have to guess on my own.
But I do not understand why B4A thinks these: "main" and "Firebasemessaging" are variables?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Ich kann Dir nur den Tip geben: Gehe deinen Code nochmal komplett durch und bilde ALLES aus dem Example nach.
Im Beispielcode funktioniert es ja! Ohne WARUNGEN. Ohne Fehler. App kann compiliert werden.
Hab es eben noch ausprobiert.

JSON, okhttp und okhttputils2 sind im Beispiel code nicht vorhanden. Wird also für FCM nicht benötigt.
 
Upvote 0

JohnnyHamburg

Member
Licensed User
Longtime User
Ich habe es nun auch noch mal von Anfang an neu gemacht und scheinbar war es nicht richtig die anderen Libraries hinzuzufügen. Jetzt meckert das Programm aber hier:

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



B4A says: " Unknown type: httpjob Are you missing a library reference?`"
 
Upvote 0

JohnnyHamburg

Member
Licensed User
Longtime User
Nachdem ich die Tutorials gefühlte 100 Mal durchgelesen habe und jedesmal auf einen Fehler bei mir gestoßen bin, habe ich es zumindest geschafft die Nachricht von B4J zur App zu schicken. Ich muss noch vieles besser verstehen, wenn ich es hier so lese. Versuche schon möglichst selbst durch lesen in den Guides und im Forum die Fehler zu finden, nur manchmal...da fragt man doch mal doof nach ;)

Vielen Dank, dass du mir geholfen hast, Manfred.
 
Upvote 0
Top