Android Question How to send firebase message to a single device

Discussion in 'Android Questions' started by Liew, Apr 30, 2019.

  1. Liew

    Liew Member Licensed User

    Dear All,

    Can anyone show me the syntax for the Job.GetRequest.SetHeader(xxxxx) in B4J where I can use token ID to send a message instead of API key.

    The reason is that I just want to direct the Firebase message to a single device that had subscribe to a topic.

    Thanks.

    Liew
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Liew

    Liew Member Licensed User

  4. DonManfred

    DonManfred Expert Licensed User

    That´s not true.

    I am using this and using the fcm token to send to. It REACHES EXTACTLY ONE Device!
     
    Last edited: Apr 30, 2019
    aidymp, Erel, BillMeyer and 1 other person like this.
  5. Liew

    Liew Member Licensed User

    Hi DonManfred, may I know the syntax parameter is "authorisation", "key="&API or other parameter. The reason is that API is for all devices suscribed to firebase. If I use the token ID obtained from firebase.token, it is not recognized. Kindly enlighten me on this. Thanks
     
  6. Jorge M A

    Jorge M A Active Member Licensed User

    This is not exactly this way. The API Key is the registration for you package name.

    Read: Integrating Firebase Services[^]
    Watch @Erel's Video: FirebaseNotifications - Push messages / Firebase Cloud Messaging (FCM)[^]

    You need to identify uniquely each device and subscribe it to same topic as id, for example.

    Then, just do that @Erel and @DonManfred already advice you: https://www.b4x.com/android/forum/t...-cloud-messaging-fcm.67716/page-3#post-433583

    It doesn't hurt to read the whole thread.
     
    asales likes this.
  7. Liew

    Liew Member Licensed User

    Thanks. I will read the link again. Maybe I didn't catch the point
     
  8. DonManfred

    DonManfred Expert Licensed User

    Ok, i´ll try again for you.

    This is the code from the Firebasetutorial on how to send to a TOPIC.
    Code:
    Private Sub SendMessage(Topic As String, Title As String, Body As String)
       
    Dim Job As HttpJob
       Job.Initialize(
    "fcm", Me)
       
    Dim m As Map = CreateMap("to"$"/topics/${Topic}"$)
       
    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


    Sub JobDone(job As HttpJob)
       
    Log(job)
       
    If job.Success Then
         
    Log(job.GetString)
       
    End If
       job.Release
    End Sub
    As the tutorial is registering the Topic general the call of this method should be something like
    Code:
    SendMessage("general""This is the Title""This is the Body")
    It will send a Notification to all devices which is running the samplecode and registering general.

    To distinguish i renamed the other sub to make it more clear here

    Code:
    Private Sub SendMessage2(id As String, Title As String, Body As String)
       
    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
    Here we do not send to a topic. Instead we are facing a specific device resp. Firebase uID.

    Each user of your app do have a specifiy Firebase uiD
    Code:
    Dim token As String = fm.Token
    The token is something like
    This token can and must be used with SendMessage2.

    the call now looks like
    Code:
    SendMessage2("vEdbT1kp2KRN3LsJPFaIkJLCQXI2""This is the Title""This is the Body")
     
    GymReaper, Jorge M A and Erel like this.
  9. Liew

    Liew Member Licensed User

    Thanks a lot DonManfred, this is what I really need
     
  10. Liew

    Liew Member Licensed User

    Hi Jorge, thanks for pointing out my mistake. As Erel first replied me, the code looks like the FCM tutorial codes and I didn't look at it further. Sorry for causing Erel, DonManfred for the extra work to explain to me further.
    So now I know the trick is to make each app subscribe to a unique topic. That means Firebase cannot send message based on token(assigned during first time device subscribe to a topic). Hope that I got it right.
     
  11. Jorge M A

    Jorge M A Active Member Licensed User

    Sorry for the late response, due to non-working day here.

    It's a bit difficult to propose something more than what @Erel and @DonManfred have already exposed so professionally in the tutorials and threads mentioned, and without knowing more in detail the characteristics of your design.

    In my case, for example, I handle three levels of topics: one "general", another related to "e-mail" and another directly to the "license plate" of the driver's specific vehicle, subscribing it in the same way as the "general" topic of the examples.

    It is worth mentioning that these data are known by the server application and stored in a database.

    Code:
    Public Sub SubscribeToTopics
        fm.SubscribeToTopic(
    "general"'you can subscribe to more topics
        Log(fm.Token)    '<<<------------ LOOK AT HERE  AND TAKE NOTE OF LONG STRING!
        
        fm.SubscribeToTopic(Starter.eMailID) 
    'you can subscribe to more topics
        fm.SubscribeToTopic(Starter.plate) 'you can subscribe to more topics
    End Sub

    Why Not?

    However, the token provided by the FirebaseMessaging library works very well, only you must have a mechanism to know it and store it in your server database, to then make the POST of the message to the specific token. Considering that this token can eventually be changed, without me knowing the cause of it. My apologies.

    TIP: You need to start the app after install, otherwise messaging service is not created.

    I have the slight suspicion that perhaps it is the structure or syntax of the message that could cause confusion, or the way you are sending it.

    I hope you can provide more information.
     
  12. Liew

    Liew Member Licensed User

    Hi Jorge,

    I think you did not get what I meant "That means Google Firebase Cloud messaging cannot send message based on token".

    Let me slightly elaborate here:

    The B4J example has a method called "Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)". In this method, the API which was meant for the package ID with Google base is used as authentication. So Firebase will send message to all devices with the package ID with respect to TOPIC subscribed.

    My question is instead of using TOPIC, can FireBase send message to ONLY the specific token ID (unique for each device). So can we ask Firebase to send to devices that fulfilled 2 conditions, a) API b) Token ID?

    I personally felt using unique TOPIC approach is a bit strange. But however if as you had said the token may change, then of course my propose idea will never work.

    As for my application I need not to use the token ID because each of my device will be attached to a panel with unique MAC. So I can just use the MAC as a unique TOPIC. With this my VPS server program can send messages to each device based on their associated MAC.

    Thanks.

    Regards,

    Liew
     
  13. Jorge M A

    Jorge M A Active Member Licensed User

    Good Luck!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice