Android Question FCM best practice?

DonManfred

Expert
Licensed User
Longtime User
Hello all,

i´m implmenting FCM into a project of a customer of mine.

Inside the Subscribetotopics i do a check if a token is available. If so then the token is send to my server and a php-script is saving the token and the device into a database.

B4X:
Public Sub SubscribeToTopics
    fm.SubscribeToTopic(starter.Phone.GetSettings("android_id")) 'you can subscribe to more topics
    Log("Token: "&fm.Token)
    If fm.Token <> "" Then
        Dim j As HttpJob
        j.Initialize("",Me)
        j.Download2("http://urltophpscript",Array As String("action","register","token",fm.Token,"deviceID",starter.Phone.GetSettings("android_id")))
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        Else
            Log(j.ErrorMessage)
        End If
        j.Release
    End If
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
    '
End Sub

At the first run of the app the token seems to be empty...

My question: Can i use a timer or a small loop using sleep to check the existence of a token?
I mean:i want to call subscribetotopics multiple-times.

For now i use this and hopefully it is ok...

B4X:
Public Sub SubscribeToTopics
    Log("SubscribeToTopics")
    fm.SubscribeToTopic(starter.Phone.GetSettings("android_id")) 'you can subscribe to more topics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
    CheckToken
End Sub
Sub CheckToken
    Log("CheckToken: "&fm.Token)
    Do While fm.Token = ""
        'Log("Sleep250")
        Sleep(250)       
    Loop
    If fm.Token <> "" Then
        Log("Register Device with Token: "&fm.Token)
        Dim j As HttpJob
        j.Initialize("",Me)
        j.Download2("http://urltophpscript",Array As String("action","register","token",fm.Token,"deviceID",starter.Phone.GetSettings("android_id")))
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        Else
            Log(j.ErrorMessage)
        End If
        j.Release
    End If

End Sub

Seems to work. Any comments in this?
 

mw71

Active Member
Licensed User
Longtime User
hi,

i think, but not sure, that the Loop is running endless:
B4X:
Do While fm.Token = ""
        'Log("Sleep250")
        Sleep(250)    
Loop


i think you can set a global Variable ()
B4X:
....
dim J_is_Finish as boolen
....
j_is_Finish=False
       Wait For (j) JobDone(j As HttpJob)
j_is_Finish=True
....

and check the Value (of j_is_Finish) in a Timer.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Looks good. This loop is fine. Even if the token will never be available it will not do any harm.
Thanx for clarification.
The question is whether you really need the token. One of the advantages of using Firebase is that in many cases you can use the topics instead.
You are probably right. I started using FCM with my knowledge about GCM.
The devices are subscribing to general (ALL) and to a topic with the deviceid. So i can use this Topic.
 
Upvote 0
Top