Android Question Send firebase notification to a specific client.

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Hello colleagues

It sounds crazy but I need to know if I can send a firebase notification to a single mobile that has my application installed?
I want to update my voting application that if a voter forgets his password, it can be sent by a notification.
Is that possible?
I am currently sending this password by email or SMS text message.
Thank you very much for your help
 

DonManfred

Expert
Licensed User
Longtime User
Send the notification to the token your app got when starting.

This token is specific for this device. Send the token to your backendserver to track them.

B4X:
Public Sub SubscribeToTopics
    topic = Starter.DeviceID
    Sleep(0)
    Do While fm.Token = Null
        Sleep(500)
    Loop
    Dim token As String = fm.Token
    Log("SubscribeToTopics fm-token = "&token)
    

    If token <> "" Then
        Log("Register Device with Token: "&token)
        Dim j As HttpJob
        j.Initialize("",Me)
        j.Download2(Starter.BridgeURL,Array As String("action","register","token",fm.Token,"deviceID",Starter.DeviceID))
        wait for (j) JobDone(job As HttpJob)
        If job.Success Then
            Dim res As String = job.GetString
            Log("Register Successfully")
            Log(job.GetString)

            job.Release
        Else
            LogColor("Error: "&job.ErrorMessage, Colors.Red)
            job.Release
        End If
    End If
    fm.SubscribeToTopic(token) 'you can subscribe to more topics
    fm.SubscribeToTopic(topic) 'you can subscribe to more topics
End Sub
 
Upvote 2

amorosik

Expert
Licensed User
Hello colleagues

It sounds crazy but I need to know if I can send a firebase notification to a single mobile that has my application installed?
I want to update my voting application that if a voter forgets his password, it can be sent by a notification.
Is that possible?
I am currently sending this password by email or SMS text message.
Thank you very much for your help

Sure
On the app you must subscribe to topic => voting_telephonenumber
And from pc, send to => voting_telephonenumber
Only one phone will be reached from the message sent by pc
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
I just tested your code for handling tokens by users

This is shown in the B4A log:

Register Device with Token: canIBWqcT2ezZuE3Kc7RNG:APA91bGuH9xSyfkl28692cCr1suMcLufSb8dOOZkg_zE8xX5JD-_jTbp8e9EBkfdZ7po8Hfib-Y8AVM-Y2ZpbVPYiiAlNg6N1hRQUC6N6UKjM7UYojyM0VB1tOG9Z

I wanted to ask you in which part of the firebase console can I see the tokens of each user?

Thank you very much my dear @DonManfred
 
Upvote 1

DonManfred

Expert
Licensed User
Longtime User
I wanted to ask you in which part of the firebase console can I see the tokens of each user?
I don´t know/think you can find them anywhere.
Probably you can get them using the firebase-rest api.

As written above i do track them by myself on my server.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
is there an example?
Yes, check the Code in the B4J Example in the Firebase Notification-Thread.
Note that you should send a notification from your server, not from a b4a app. Though it is possible.
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
thank you my friend @DonManfred

I already found this code of yours that works perfectly

B4X:
Private Sub SendMessage(id As String, Title As String, Body As String)
    Dim API_KEY As String="Your Key"   
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"${id}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Dear friend @DonManfred

When sending a Firebase notification with this code in B4A, I imagine that it would just replace the ID with the token that I have saved in my database.

Would the Firebase notification be sent to only that Token?

I thank you very much for your answer.
 
Upvote 0
Top