Android Question java.lang.object cannot be cast to b4xpagesmanager

Muhamad Kamal

Member
Licensed User
Hi!

I've received java.lang.ClassCastException error in my app before FirebaseMessaging Service_Start sub is triggered.



Every time the app receives a Firebase message the above error will appear then only the Service_Start sub is triggered.

Does anyone experience the same problem? The app can still run after the above error appeared.

I used the same code for my other app without B4XPages and it works just fine. For this particular app I'm using B4XPages and when the message arrived I trigger Main activity to open.


my FirebaseMessaging code:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
    Private smiley As Bitmap
    
    Public message_received As Boolean
    
End Sub

Sub Service_Create
    Try
        fm.Initialize("fm")
        smiley = LoadBitmapResize(File.DirAssets, "icon.png", 24dip, 24dip, False)
    Catch
        'ToastMessageShow(LastException.Message,True)
        Log("FirebaseMessaging>Service_Create: " & LastException.Message)
    End Try
End Sub

Public Sub SubscribeToTopics
    Try
        fm.SubscribeToTopic("general")
        fm.SubscribeToTopic("orders")
    Catch
        'ToastMessageShow(LastException.Message,True)
        Log("FirebaseMessaging>SubscribeToTopics: " & LastException.Message)
    End Try
End Sub

Sub Service_Start (StartingIntent As Intent)
    Try
        If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
        Sleep(0)
        Service.StopAutomaticForeground 'remove if not using B4A v8+.
    Catch
        'ToastMessageShow(LastException.Message,True)
        Log("FirebaseMessaging>Service_Start: " & LastException.Message)
    End Try
End Sub

Sub OrderNotification(title As String, body As String)
    Try       
        'parse body
        'format: app-code||recipient||order_id||details
        Dim body_array() As String
        body_array=Regex.Split("\|\|",body)
        
        'only proceed if message is directed to current app & user
        If body_array(0).ToUpperCase = "BAZAARNEGARA-BRAND" And body_array(1) = Main.gUser Then
            
            Dim n As NB6
            
            Config.GetSound
            Dim rp As FileProvider
            rp.Initialize
            Dim folder As String = rp.SharedFolder
            Dim FileName As String = Main.gSoundName
            'copy the file to the shared folder
            If File.Exists(folder, FileName) = False Then File.Copy(File.DirAssets, FileName, folder, FileName)

            n.Initialize("custom sound", Application.LabelName, "HIGH").SmallIcon(smiley)
            Dim cs As CSBuilder
            n.BadgeIconType("LARGE")
            n.LargeIcon(smiley)
            n.BigTextStyle(title, cs.Initialize.Color(Colors.Red).Append("Tempahan Baru").PopAll, body_array(3))
            'disable the default sound
            n.SetDefaults(False, True, True)
            'set custom sound
            n.CustomSound(rp.GetFileUri(FileName))

            n.Build(title, "Buka untuk melihat keterangan...", "Main", Main).Notify(1)
        End If
    Catch
        'ToastMessageShow(LastException.Message,True)
        Log("FirebaseMessaging>OrderNotification: " & LastException.Message)
    End Try
End Sub

Sub SiteNotification(title As String, body As String)
    Try
        Dim n As NB6

        n.Initialize("default", Application.LabelName, "HIGH").SmallIcon(smiley)
        Dim cs As CSBuilder
        n.BadgeIconType("LARGE")
        n.LargeIcon(smiley)
        n.BigTextStyle(title, cs.Initialize.Color(Colors.Red).Append("Pengumuman").PopAll, body)

        n.Build(title, "Buka untuk melihat mesej...", "Main", Main).Notify(2)
    Catch
        'ToastMessageShow(LastException.Message,True)
        Log("FirebaseMessaging>SiteNotification: " & LastException.Message)
    End Try
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Try
        Log("Message arrived")
        Log($"Message data: ${Message.GetData}"$)
        
        message_received = True
        
        Dim title As String = Message.GetData.Get("title")
        If title.ToLowerCase.Contains("makluman") Then
            SiteNotification(title, Message.GetData.Get("body"))
        Else
            OrderNotification(title, Message.GetData.Get("body"))
        End If
        
    Catch
        'ToastMessageShow(LastException.Message,True)
        Log("FirebaseMessaging>fm_MessageArrived: " & LastException.Message)
    End Try
End Sub

Sub Service_Destroy

End Sub
 

Muhamad Kamal

Member
Licensed User
There is no error log related to this error.. I could not trace which line produces this error as I already put all sub which necessary sub name on every log message but nothing was triggered..

I did the project clean up as well few times with no luck
 
Upvote 0

Muhamad Kamal

Member
Licensed User
The error triggered right after the app receive the fm msg then only the debug point in FirebaseMessaging service triggered.. and when I went thru the debug steps no error was produced
 
Upvote 0

Muhamad Kamal

Member
Licensed User
I still couldn't find where is it coming from.. even if the activity is paused in the background when new msg arrived it will prompt this toast msg first before the FirebaseMessaging service is invoked
 
Upvote 0

Muhamad Kamal

Member
Licensed User
after many testing and debugging were made here is what I found out so far:

A1. this new b4xpages project is made for new client using their domain name for app name (biz.newdomain)
A2. I've created 3 apps in firebase for this client's app (eg: biz.newdomain, biz.newdomain.user, biz.newdomain.order)
A3. I was testing the order app so I'm using the google-services.json downloaded from the firebase console for this app
A4. then this problem occurred

so for the debugging purposes, I did the following steps:

B1. I created new firebase project using new google account
B2. then I created the 3 apps in firebase using different domain (eg: com.diffdomain, com.diffdomain.user, com.diffdomain.order)
B3. downloaded the order app google-services.json file
B4. problem gone

The com. package config did not produce the problem but the biz. package config does produce this problem. If I switch back to the biz. config with its .json file this problem still persists.

Do you have any idea why is this happening?
 
Upvote 0

Muhamad Kamal

Member
Licensed User
further test..

B5. I created one of the app (biz.newdomain.order) using B1 account
B6. downloaded the .json file, set build config using biz.newdomain.order
B7. this does not produce the error


Might be something wrong with the Firebase project that I've created earlier.. but my other apps using this Firebase account are still running (apps that are not using B4XPages tho)

?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…