Android Question Firebase: Waiting for a token, waiting for subscriptions to complete etc?

Mashiane

Expert
Licensed User
Longtime User
Ola

My app is greatly dependent on waiting for processes to start and finish, like for example, waiting for a token before I am able to send a message, waiting for a subscription to take place until I can move to the next screen. I intend to use a loading progress screen until the process is complete,

Thing is, after start, the firebase token is not available immediately, and with

B4X:
'CallSubDelayed2(FirebaseMessaging, "SubscribeToTopic", "devicex")

the subscription is actually sent to a que and will be available later.

I have seen some code here to start an service and wait for it to complete, https://www.b4x.com/android/forum/t...a-service-or-activity-and-wait.79856/#content

and am trying to implement it. Now..

Q1. How do I wait for a firebase token to be available?
Q2. How do I wait for a subscription to complete?

I have thought of using httputils and a post request however before I do that (as described on this post: https://www.b4x.com/android/forum/t...ubscribe-and-unsubscribe-topic.88828/#content), however

Is it possible to achieve this without having to write extra code?

Thanks...
 

Alexander Stolte

Expert
Licensed User
Longtime User
'CallSubDelayed2(FirebaseMessaging, "SubscribeToTopic", "devicex")
simple example:
just call your function with "wait for" after that it will only be executed if the wait for has a return value
B4X:
Wait For (fa_rest.signUpEmailPW("[email protected]","Test123!",True)) complete (root As Map)
Module Code:
Public Sub signInEmailPW(email As String, password As String,returnSecureToken As Boolean) As ResumableSub
    
    Dim url As String = $"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${API_KEY}"$
    
    Dim json As JSONGenerator
    json.Initialize(CreateMap("email":email,"password":password,"returnSecureToken":returnSecureToken))
    Dim j As HttpJob : j.Initialize("",Me)
    j.PostString(url,json.ToString)
    j.GetRequest.SetContentType("application/json")
    
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        Else
        Log(j.ErrorMessage)
    End If
j.Release
Return True 'done
End Sub

wich code are you using to get a token?
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
The fire-base service has built in methods...

B4X:
Public Sub SubscribeToTopic (Topic As String)
    fm.SubscribeToTopic(Topic)
End Sub


Public Sub UnsubscribeToTopics(topic As String)
    fm.UnsubscribeFromTopic(topic)
End Sub

To get a token, one executes

B4X:
log(fm.token)

All these calls do not execute immediately or rather need to propagate first. What id like to achieve is wait until a token is retrieved, wait until a subscription is made and also wait until an un-subscribe method is execute.

The methods discussed in the other posts are for "outside execution", so i wanted to avoid having to write extra code to do that.

For example, sending a message for firebase is not built it, one needs to write a post and wait for it to finish if they follow that approach.

If its possible for the built in methods to use some kind of waiting mechanism, all is good for my case. If not possible then I guess I have to code the post request, though thats not my outlook.
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Waiting for a firebase token

It seems when the app is being ran for the first time and it needs a token. You just never get it, so use sleep.

This is on the FirebaseMessaging service

B4X:
Sub Service_Create
    Log("Firebase create...")
    fm.Initialize("fm")
    Do While fm.token = ""
        Log("Sleep250")
        Sleep(250)
    Loop
    Token = fm.token
End Sub

I still wish one could use Wait For to achieve this.


Update:

After many many tests, running the app for the first time never gives one a token. The solution above works when the app is already installed and has ran before.
 
Last edited:
Upvote 0
Top