B4J Code Snippet [B4X] - Get subscribed Firebase Topics From Firebase

Discussion in 'B4J Code Snippets' started by aaronk, Jan 11, 2019.

  1. aaronk

    aaronk Well-Known Member Licensed User

    The following code will help get the topics a firebase token is subscribed to.

    It will send a request to Firebase and ask for the reply. The reply is in a JSON file.

    Add the following code to your project, Add your Firebase project Key and Add the token, then call RequestTopics.

    Key can be found in firebase console:
    Your project -> settings -> Project settings -> Cloud messaging -> Server Key
    (Be careful when finding key, dont use web api key its different.)

    This comes from the B4A/B4i app which you want to check.

    I called this sub in the fm_FCMConnected sub.
    Note, reading many forum posts, some say it sometimes doesn't return a value, but it seems to return a value everytime for me when using it in this sub.

    Private Sub GetToken As String
    Dim no As NativeObject
    Dim token As NativeObject = no.Initialize("FIRInstanceID").RunMethod("instanceID"Null).RunMethod("token"Null)
    If token.IsInitialized Then Return token.AsString Else Return ""
    End Sub
    You can get it from the Firebase Notifications object:

    I recommend calling the following code from B4J and then sending it to your B4/B4i apps (if your B4A/B4i app need it) since it contains API keys etc. but it should work on B4A/B4i as well if really required.

    Requires JSON & jOKHTTPUtils2 Library's.

    Sub RequestTopics
    Dim Key As String
    Dim Token As String
    Dim job1 As HttpJob
    ' Get Key from Firebase project
    ' Token comes from B4A/B4i App
        Token = "TOKEN FROM B4A/B4i HERE"
    ' No need to modify the URL
        Dim URL As String = "https://iid.googleapis.com/iid/info/" & Token & "?details=true"
    "JOB1", Me)
    "Authorization""key=" & Key)
    Wait for JobDone (Job As HttpJob)

    Log("Success = " & Job.Success)
    If Job.Success = True Then
    ' Reply from Firebase
    ' Send the JSON reply to the next sub for processing
    End If
    End Sub

    Sub GetTopicsFromJSON(value As String)
    Dim parser As JSONParser
    Dim root As Map = parser.NextObject
    Dim parser As JSONParser
    Dim root As Map = parser.NextObject
    Dim applicationVersion As String = root.Get("applicationVersion")
    Dim connectDate As String = root.Get("connectDate")
    Dim application As String = root.Get("application")
    Dim scope As String = root.Get("scope")
    Dim authorizedEntity As String = root.Get("authorizedEntity")
    Dim rel As Map = root.Get("rel")
    Dim Topics As Map = rel.Get("topics")
    Dim connectionType As String = root.Get("connectionType")
    Dim platform As String = root.Get("platform")

    Log($"applicationVersion = ${applicationVersion}"$)
    Log($"connectDate = ${connectDate}"$)
    Log($"application (Package Name) = ${application}"$)
    Log($"scope = ${scope}"$)
    Log($"authorizedEntity = ${authorizedEntity}"$)
    Log($"connectionType = ${connectionType}"$)
    Log($"platform = ${platform}"$)
    Log($"Number of Topics = ${Topics.Size}"$)
    Log(" ")
    ' The following shows the topics in the Map
        ' Key = Topic
        ' Value = Date it was added
        Log("Topic - Date Subscribed:")
    For i = 0 To Topics.Size - 1
    Log(Topics.GetKeyAt(i) & " " & Topics.GetValueAt(i))

    End Sub
  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