Android Question Firebase token

DonManfred

Expert
Licensed User
Longtime User
B4X:
Public Sub SubscribeToTopics
   fm.SubscribeToTopic("general") 'you can subscribe to more topics
   Log($"Token(${fm.Token})"$)   
End Sub
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
However, in my experiments, I see that the token is null at the position you have put the Log. Instead, for fm.Token, I get a string like:

fzhlJrML5nQ:APAErbGGvA-V4Xxx-vj7yQjPzAAqgHHe5JMbknZrozZOFSN9QHbTkt6c-boMmNt-QWPZyVsgZHINW0N7tTXKc69MYTq29nXglLTtJPp09glKucb5-ifEKWrH_TZdXJdkeK74_v9WM37x

inside the: "Sub fm_MessageArrived (Message As RemoteMessage)".

Thanks again.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi.
I wrote this post just to understand why the TokenRefresh seems not to work. For my use this is not actually a problem, as pointed out in what follows.
I tried to insert the fm_TokenRefreh event, like in the following code. My purpose is to set a variable declared in Main module: Main.Token. The log shows that:
1) During SubscribeToTopics the token is empty
2) No call to TokenRefresh (so, I ask myself what I am missing..)
3) In MessageArrived the Token exists. (This is enough, for my use, anyway)
The code:
B4X:
Sub Process_Globals
  Private fm As FirebaseMessaging
  'Dim RefreshDataFlag As Boolean
End Sub
Sub Service_Create
  fm.Initialize("fm")
  Log("Messaging_Create")
End Sub
Public Sub SubscribeToTopics
  fm.SubscribeToTopic("nessuno") 'you can subscribe to more topics
  Log("In Subscribe " & $"Token(${fm.Token})"$)  
End Sub
Sub Service_Start (StartingIntent As Intent)
  If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
  Log("Messaging start")
 End Sub
Sub fm_TokenRefresh
   Log("In Refresh " & $"Token(${fm.Token})"$)
 Main.Token=fm.Token
End Sub
Sub fm_MessageArrived (Message As RemoteMessage)
  
  Main.MessageArrived=Message.GetData.Get("message")
  Main.Token=fm.Token
  Log("In Arrived " & $"Token(${fm.Token})"$) 
  If IsPaused(Main) Then
 StartActivity(Main)
 CallSub(Main,"SaveData")
  Else
    CallSub(Main,"SaveData")
  CallSub(Main, "RefreshData")
  End If
  
  Dim n As Notification
  n.Initialize
  n.Icon = "icon"
  n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("message"),Main)
  n.Notify(1)
  
End Sub
The Log:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Resume
resume :
resume :
** Service (firebasemessaging) Create **
Messaging_Create
** Service (firebasemessaging) Start **
Messaging start
In Subscribe Token() <<<<<<<<< here token is empty
** Service (firebasemessaging) Start **
In Arrived Token(ckjOv0zMpZc:APA91bG7rNQ3XemLfL2VmVRCn5dzB9Xf6eHKwp6A6oLIp3CtlzX_OBf9USENVyf_xjMTzeJrM6oIbT9mubv7BlDXP4pFxjn5J9Xop9FHMYgTy6OujimHO6ThwGkKPWDoVSF7-azyCFDQ)

Thanks for attention.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
With this code I get the token every time:

B4X:
Sub Service_Create
   fm.Initialize("fm")
   UpdateFCMToken 'directly after fm.initialize
   Log (fm.Token)
End Sub

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

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all. Thanks for help.
By the way I observe that I called SubscribeToTopic but in the Starter service, as I saw done somewhere (it was not my idea for sure).
In Starter service I have:
B4X:
Sub Service_Create
 'This is the program entry point.
 'This is a good place to load resources that are not specific to a single activity.
  CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub
Now, if I understand well the log in my previous post, it seems that the Starter Service_Create is called before the FirebaseMessaging_Create and so the subscription done inside the starter Service_Create could now work because the FirebaseMessaging is not yet created. The call to SubscribeToTopics is "delayed" but .. perhaps better to move it to the FirebaseMessaging Service.
Thanks for attention.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all. I submit the following, either for confirming it by some guy more expert than I, or to help somebody else, in case of need.
From what I see the token can be obtained according to what follows, assuming that you have a Starter Service.
This issue is crucial if you need the token without receiving any notification.
Inside the Starter Service:
B4X:
Sub Service_Create
 'This is the program entry point.
 'This is a good place to load resources that are not specific to a single activity.
  'CallSub(FirebaseMessaging, "SubscribeToTopics") ' seems not useful
 StartService(FirebaseMessaging) ' put this
End Sub
Inside FirebaseMessaging Service:
B4X:
Sub Service_Create
  fm.Initialize("fm")
  Log("Messaging_Create")
  SubscribeToTopics
End Sub
Public Sub SubscribeToTopics
  fm.SubscribeToTopic("nessuno") 'you can subscribe to more topics
  Log("In Subscribe " & $"Token(${fm.Token})"$)  
End Sub
Sub fm_MessageArrived (Message As RemoteMessage)
  Main.MessageArrived=Message.GetData.Get("message")
  Main.Token=fm.Token
  Log("In Arrived " & $"Token(${fm.Token})"$)
  If IsPaused(Main) Then
 StartActivity(Main)
 CallSubDelayed(Main,"SaveData")
  Else
    CallSubDelayed(Main,"SaveData")
  CallSubDelayed(Main, "RefreshData")
  End If
 
  Dim n As Notification
  n.Initialize
  n.Icon = "icon"
  n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("message"),Main)
  n.Notify(1)
 
End Sub

The Log confirms the above schema,showing obviously same token during Subscription and Message arrival.

** Service (firebasemessaging) Create **
Messaging_Create
In Subscribe Token(cMingPURd9c:APA91bFVcUWGBk5aCN-DuNZX_FWAMpqx4bF0t9l6Hsei4QDHv_bL4ErdDG32tavukes9OIfu0C3i4ccpx-... etc.)
** Service (firebasemessaging) Start **
In Arrived Token(cMingPURd9c:APA91bFVcUWGBk5aCN-DuNZX_FWAMpqx4bF0t9l6Hsei4QDHv_bL4ErdDG32tavukes9OIfu0C3i4ccpx- .. etc.)
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Yes KMatle, ok. The subject was not the fact that the token is (obviously, as I wrote) the same, but that, to get it, using the Starter thread call, it is necessary to follow the schema that I reported. Your indication was useful, and I am simply pointing out very small details, probably trivial for you. Thanks.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
probably trivial for you

Nah :) It took me some days to get the schema how to create an app, do all the Firebase Console settings and use it. Then I had some issues with my app not receiving messages while in the background (energy settings of my phone). Just wanted to understand what your problem is.
 
Upvote 0
Top