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

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
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.)

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

------------------------------------------------------------------------------
B4i
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.

B4X:
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

------------------------------------------------------------------------------
B4A
You can get it from the Firebase Notifications object:
B4X:
Log($"Token(${fm.Token})"$)

------------------------------------------------------------------------------

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.

B4X:
Sub RequestTopics
   
    Dim Key As String
    Dim Token As String
   
    Dim job1 As HttpJob
   
    ' Get Key from Firebase project
    Key = "KEY FROM FIREBASE HERE"
   
    ' 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.Initialize("JOB1", Me)
   
    job1.PostString(URL,Null)
    job1.GetRequest.SetHeader("Authorization", "key=" & Key)
    job1.GetRequest.SetContentType("application/json;charset=UTF-8")
   
    Wait for JobDone (Job As HttpJob)

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

Sub GetTopicsFromJSON(value As String)
    Dim parser As JSONParser
    parser.Initialize(value)
    Dim root As Map = parser.NextObject
    Dim parser As JSONParser
    parser.Initialize(value)
    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))
    Next

End Sub
 
Top