Android Question FireBase Messaging TokenID (SOLVED)

walterf25

Expert
Licensed User
Longtime User
Hi All, i am working on a personal project i started a few years ago, I finally found some time to get somethings going but I am a little confused about a few things regarding FireBase Messaging TokenID.

I have a B4J Non_UI application running in a google cloud virtual machine which receives requests from my android app, the information i pass to the B4J app is the TokenID and message so that it can be distributed to a single device based on the unique TokenID, the B4J app when it receives these requests via POST method and then makes the calls to the Google API

The TokenID string is saved in a remote database, as I understand this string should be a unique String assigned to every device.

Below is part of the code i am using.
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim start As Long = DateTime.Now
    resp.ContentType = "text/html;charset=utf-8"
    response = resp
    Dim messageto As String = req.GetParameter("messageto")
    Select messageto
        Case "Chofer"
            
        Case "Owner"
        SendMessageToOwnerDevice(req.GetParameter("fcmtoken"), req.GetParameter("data"))

        Case "Cliente"
            
    End Select
    StartMessageLoop 'we need this here, for the wait for
End Sub

B4X:
Private Sub SendMessageToOwnerDevice(FCMToken As String, datamessage As String)
    Dim Job1 As HttpJob
    Job1.Initialize("SendMessageOwner", Me)
  
    DateTime.DateFormat="dd.MM.yyyy"
    Dim DatumZeit As String
    DatumZeit=DateTime.Date(DateTime.Now)
    DatumZeit=DatumZeit & " " & DateTime.Time(DateTime.Now)
  
  
    Dim m As Map = CreateMap("to": $"${FCMToken}"$)
    Dim data As Map = CreateMap("data": datamessage)
    m.Put("data", data)
    m.Put("content_available", True)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job1.Tag=jg
    Log(jg.ToString)
    Job1.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job1.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job1.GetRequest.SetHeader("Authorization", "key=" & Main.API_KEY)
End Sub

This part is self explanatory and seems to work just fine.
My confusion is that when I test and send a message to to my device I receive the notification just fine inside the FireBaseMessaging Service of my Android app, but when I test with my cousin's device he never receives the notification. I just figured that every time he opens the app on his device the TokenID String is different everytime, so it seems like when the B4J app sends the notification it sends it to the TokenID that was initially saved when he signs up with his email and password.

Is this normal? should I not be using the TokenID to send individual notifications to single devices, is there any other way to accomplish this, am i doing this the wrong way?

Thoughts?

Walter
 

walterf25

Expert
Licensed User
Longtime User
It is easier to send by Topic. Each device will have a unique subscription of topic.
unfortunately that's not an option for this project, I need to be able to send messages to individual devices.

Walter
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Is this normal?
Yes it is normal, Token id is changeable, but not much frequent like your cousin's device!

is there any other way to accomplish this
Many workarounds can be done according your imagination, the best one is your method, also the 2nd easiest alternative way is @aeric suggests: using topic because it will be unique & fixed for a long time.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Thanks for your input, i found out why the TokenID was changing so much, it turns out that my cousin was deleting the application from his phone every time before installing the new versions i've been sending to him.

Also i didn't realize that there is a Sub fm_TokenRefresh (Token As String) function, so i am taking advantage of this and updating the TokenID in case it changes.

Thanks all for your feedback.

Walter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…