Android Question FirebaseNotifications - unsubscribes all topics

zolive33

Member
Licensed User
Hello,

How can I do to unsubscribe all topics? It seems to me to use the deleteInstanceId method, but I do not know how to do it.

Can you help me?

Regards.
 

zolive33

Member
Licensed User
There is no API to unsubscribe from all topics. You can instead store the subscribed topics in a text file and then unsubscribe from all of them.
Thanks for your reply, Erel.

The Firebase documentation explains that the deleteInstanceId() method "Resets Instance ID and revokes all tokens". On the StackOverflow forum I found this post: "However, if you want to stop receiving from all the topics and then the token is not useful at all, you can call FirebaseInstanceId.deleteInstanceId() (reference: deleteInstanceId()) that will reset the instance id and than again you can subscribe to new topics from the new instance id and token."

link : http://stackoverflow.com/questions/...om-all-topics-at-once-from-firebase-messaging

It seems to me that this method revokes all subscriptions, isn't it?
 

zolive33

Member
Licensed User
With FireBase Messaging the method is a little bit different :

B4X:
    Dim jo As JavaObject
  
    Try
        jo = jo.InitializeStatic("com.google.firebase.iid.FirebaseInstanceId").RunMethod("getInstance", Null)
        jo.RunMethod("deleteInstanceId", Null)
    Catch
        Log(LastException)
    End Try
But returns a java.io.IOException MAIN_THREAD, because deleteInstanceId() can't be called on the main thread.
 

zolive33

Member
Licensed User
Here is the sample code :

B4X:
#Region  Service Attributes
    #StartAtBoot: false
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private fm As FirebaseMessaging
    Dim Thread1 As Thread
End Sub

Sub Service_Create
  
    fm.Initialize("fm")
    Thread1.Initialise("Thread1")
  
End Sub

Public Sub subscribeToTopic

    fm.SubscribeToTopic("DebugTopic")
    Log ($"Token : ${fm.Token}"$)
  
End Sub

Public Sub removeToken()
  
    Dim args(0) As Object
    Thread1.Name = "B4A Thread 1"
    Thread1.Start(Null, "ThreadSub1", args)
  
End Sub

Sub ThreadSub1

    Dim jo As JavaObject

    Try
        jo = jo.InitializeStatic("com.google.firebase.iid.FirebaseInstanceId").RunMethod("getInstance", Null)
        jo.RunMethod("deleteInstanceId", Null)
    Catch
        Log(LastException)
    End Try

End Sub

Sub Thread1_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
  
    If Not(fail) Then
        Log ($"Thread1 Ended with error : ${error}"$)
    Else
        Log ("Thread1 Ended without error")
    End If

End Sub

Sub fm_MessageArrived (Message As RemoteMessage)

   Log("Message arrived")
   Log($"Message data: ${Message.GetData}"$)
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
   n.Notify(1)

End Sub

Sub Service_Destroy
End Sub
To recreate (if necessary) Token and add a topic :
B4X:
CallSubDelayed(FirebaseMessaging, "subscribeToTopic")
To remove Token and all topics :
B4X:
CallSubDelayed(FirebaseMessaging, "removeToken")
 
Last edited:
Top