Android Question FireBaseService Sub not exists

ilan

Expert
Licensed User
Longtime User
hi

i have a lot firebase crashes and suddenly i think i know why.

making some simple tests gave me this:

on starter_create i am asking to subscribetotopics but it says the the sub doesnot exists

B4X:
Sub Service_Create
    logs.Initialize
    #if RELEASE
        logcat.LogCatStart(Array As String("-v","raw","*:F","B4A:v"), "logcat")
    #end if

    shared = rp.GetSafeDirDefaultExternal("shared")  
   
    If File.Exists(File.DirInternal,"userid.txt") Then
        userid = File.ReadString(File.DirInternal,"userid.txt")
    Else
        userid = createrandomuser(12)
        Log("userid: " & userid)
        File.WriteString(File.DirInternal,"userid.txt",userid)
    End If  
   
    Try
        If SubExists(FirebaseMessaging, "SubscribeToTopics") Then
            CallSubDelayed(FirebaseMessaging, "SubscribeToTopics") 'notification test
        Else
            Log("not exists")  
        End If
    Catch
        Log(LastException)
    End Try
End Sub

this is my firebaseservice:

B4X:
'FirebaseMessaging
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals  
   Private fm As FirebaseMessaging
   Private Thread1 As Thread 'ignore
End Sub

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

Public Sub SubscribeToTopics
    Try
        Log("subscribe to topics called")
        fm.SubscribeToTopic("buysmartnew") 'you can subscribe to more topics
        fm.SubscribeToTopic(Starter.userid)
    Catch
        Log(LastException)
    End Try
End Sub

Sub Service_Start (StartingIntent As Intent)
   If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
   Sleep(0)
   Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
   
    Try
        Dim title As String
        title = Message.GetData.Get("title")
        title = title.Replace(" ","")
       
        If title.Contains("unsubscribe") Then 'new version
             Log("call unsubsribe to all topics")
             removeToken
        else if title = "chat" Then 'received chat message
            'do something              
        Else
           Dim n As Notification
           n.Initialize
           n.AutoCancel = True
           n.Light = True
           n.Sound = False
           n.Vibrate = True          
           n.Icon = "icon"
           n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
           n.Notify(1)          
        End If      
    Catch
        Log(LastException)
    End Try
End Sub

Sub Service_Destroy

End Sub

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

Sub ThreadSub1 'ignore
    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")
        Sleep(1000)
        SubscribeToTopics
    End If
End Sub

why do i get FALSE in logs?

*** Service (starter) Create ***
not exists
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Service (starter) Destroy (ignored)**
 

ilan

Expert
Licensed User
Longtime User
ok ignore my first question i know why i get FALSE. the service is in the background and CallSubDelayed starts the service and then called the sub so i cannot check if a sub exists if the module is in the background but even if i remove the if sub exists the service doesn't starts always when the starter service is created.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok after updating the recommended packages from the sdk manager everything runs fine. thank you so much erel for your awesome support!
best support i have ever seen!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…