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

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.

B4X:
Public fm As FirebaseMessaging
fm.Token

You can more read over this here.

B4X:
Private Sub SubscribeToTopic(Token() As String, TopicName As String) As ResumableSub
    
    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)
 
    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

Private Sub UnsubscribeTopic(Token() As String, TopicName As String) As ResumableSub
    
    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)
 
    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

Greetings
 
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime 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.
 

Alexander Stolte

Expert
Licensed User
Longtime User
EDIT to this:
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

if you want to subscribe only one user to a topic:
B4X:
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
 
Top