Android Question [SOLVED] FCM token availability

udg

Expert
Licensed User
Hi all,
in the past I sucessfully used FCM for messages destined to group of users (i.e. topics). Today I'm experimenting with messages intended for a single user (i.e. token).
What I'm experiencing is that the token seems to become available only on a second launch of the application (or anyway not soon after the app is started).
Once the token becomes available, it suffice to disinstall the app and then istall it again to go back to square one where no token is available.
My code (in FirebaseMessaging service) is as follows:
B4X:
Sub Service_Create
    fm.Initialize("fm")
    Log("Token: "&fm.Token)
end sub
Should I introduce a wait for fm.Isinitialized in service start, rely on some event (not TokenRefresh, since it doesn't raise at this time) or simply buy a new phone (mine is Android 4.4)?
Any other idea? TIA
 

KZero

Active Member
Licensed User
try adding sleep(0)
B4X:
Sub Service_Create
    fm.Initialize("fm")
   sleep(0) 
    Log("Token: "&fm.Token)
end sub
 
Upvote 0

DonManfred

Expert
Licensed User
I do it this way (but in the RegisterTopic sub to be sure i have a Token)

B4X:
Sub Service_Create
    fm.Initialize("fm")
    While fm.Token = ""
        Sleep(250)
    Loop
    Log("Token: "&fm.Token)
end sub

Sometimes it can last a few seconds until the Token is available. Especially in the first run.
 
Upvote 0

KZero

Active Member
Licensed User
I do it this way (but in the RegisterTopic sub to be sure i have a Token)

B4X:
Sub Service_Create
    fm.Initialize("fm")
    While fm.Token = ""
        Sleep(250)
    Loop
    Log("Token: "&fm.Token)
end sub

Sometimes it can last a few seconds until the Token is available. Especially in the first run.

but if the device doesn't have Google play or google account it will be an infinite loop

I think this code is a safer option
B4X:
Sub Service_Create
    fm.Initialize("fm")
   For i = 0 To 5
        If fm.Token<>"" Then Exit
        Sleep(250)
    Next
Log("Token: "&fm.Token)
 
Upvote 0

udg

Expert
Licensed User
Thank you all. I'm glad you confirmed that it's not my old phone the main cause of the problem. It is "normal" that, at least on first app execution, it takes some time to have a token available.
I will postpone the "token available" check until the time I really need it and set an exit strategy in case it will still be missing after some generous waiting time.
 
Last edited:
Upvote 0

cirollo

Active Member
Licensed User
hi!
I'm using a backoffice app (like that in the B4J example) to send notifications.
I guess I need to send to this app the token of all devices involved. This is needed to target a specific device.
Any idea or example?
 
Upvote 0

udg

Expert
Licensed User
Hi cirollo, it depends on how you organize your operation.
In my case I had the mobile app to send to a server the token (and other data). But you could program the B4J broker in order to listen to a public topic where mobile apps sends their tokens (along with any other specific identification data).
Depending on your needs other approaches could be more efficient or fit better.
 
Upvote 0
Top