B4J Code Snippet [B4X] Firebase Cloud Messaging Subscribe and Unsubscribe Topic

Discussion in 'B4J Code Snippets' started by Alexander Stolte, Jan 26, 2018.

  1. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    With this 2 Functions you can Subscribe and Unsubscribe Users from a Topic. This is Useful for Server Apps.

    You need the Token from the User, the Token is the InstanceID and this ID is unique for each device, you can get this ID in the "Firebase Cloud Messaging" lib.

    Code:
    Public fm As FirebaseMessaging
    fm.Token
    You can more read over this here.

    Code:
    Private Sub SubscribeToTopic(Token() As String, TopicName As String)
         
        
    Dim Job As HttpJob
        Job.Initialize(
    "fcm", Me)
        
    Dim m As Map = CreateMap("to"$"/topics/${TopicName}"$"registration_tokens":Token)
     
        
    Dim jg As JSONGenerator
        jg.Initialize(m)
     
        Job.PostString(
    "https://iid.googleapis.com/iid/v1:batchAdd", jg.ToString)
        Job.GetRequest.SetContentType(
    "application/json")
        Job.GetRequest.SetHeader(
    "Authorization""key=" & API_KEY)
     
    End Sub

    Private Sub UnsubscribeTopic(Token() As String, TopicName As String)
         
        
    Dim Job As HttpJob
        Job.Initialize(
    "fcm", Me)
        
    Dim m As Map = CreateMap("to"$"/topics/${TopicName}"$"registration_tokens":Token)

        
    Dim jg As JSONGenerator
        jg.Initialize(m)
     
        Job.PostString(
    "https://iid.googleapis.com/iid/v1:batchRemove", jg.ToString)
        Job.GetRequest.SetContentType(
    "application/json")
        Job.GetRequest.SetHeader(
    "Authorization""key=" & API_KEY)
     
    End Sub
    Greetings
     
    Last edited: Feb 27, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    It took me a moment to understand. This code can be used outside of the app that receives the messages.
     
  3. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    You can unsubscribe a device from a topic with his Instance id or you can subscribe a device to a topic, i found it is a security feature if it runs on a Server, because, if someone manipulate my code, then he can subscribe to a topic he want.
     
  4. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    EDIT to this:
    if you want to subscribe only one user to a topic:
    Code:
    Private Sub SubscribeToTopic(Token As String, TopicName As String)
      
        
    Dim Job As HttpJob
        Job.Initialize(
    "fcm", Me)
     
        Job.PostString(
    "https://iid.googleapis.com/iid/v1/" & Token & "/rel/topics/" & TopicName, "")
        Job.GetRequest.SetContentType(
    "application/json")
        Job.GetRequest.SetHeader(
    "Authorization""key=" & API_KEY)
     
        
    Wait For (Job) JobDone(Job As HttpJob)
      
        
    If Job.Success Then
            
    Log(Job.GetString)
            
    Log("sub war erfolgreich!!!")
        
    Else
            
    Log("sub war nicht erfolgreich")
        
    End If
        Job.Release
     
    End Sub
     
    Erel likes this.
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