Android Question Notification error

pliroforikos

Active Member
Licensed User
Hello eveybody

I have a problem when receiving notifications to my app.
When it is open everything works ok and notifications comming and show as it is expected.

But when app closed/killed i see in the log file that message arrived but error occured and nothing showing on android screen. Tried in several mobiles.
Message arrived
Message data: {body=Hello Students, sound=default, title=mySchool}
b4xpages_getmanager (java line: 56)
java.lang.ClassCastException: java.lang.Object cannot be cast to b4a.mySchool.b4xpagesmanager
at b4a.mySchool.b4xpages._getmanager(b4xpages.java:56)
at b4a.mySchool.b4xpages._mainpage(b4xpages.java:100)
at b4a.mySchool.firebasemessaging._fm_messagearrived(firebasemessaging.java:162)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA$1.run(BA.java:352)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6138)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    
    n.Initialize
    n.Initialize2(n.IMPORTANCE_DEFAULT)
    n.Icon = "icon"
    n.OnGoingEvent = False
    'n.Sound = True
    n.AutoCancel = True
    
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    
    B4XPages.MainPage.BadgeNumber = B4XPages.MainPage.BadgeNumber + 1
    n.Notify(1)
    msgs = msgs + 1
    
    B4XPages.MainPage.reloadSystemMenu(B4XPages.MainPage.BadgeNumber)
    
End Sub

Any idea? Thanks in advanced...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Error is expected.
If your app wasn't running when the message was received, then the FirebaseMessasingService will start. At this point the main activity wasn't started so you cannot access B4XPages.
You can check it with:
B4X:
If B4XPages.IsInitialized = False Then
 'no pages
If the number is important then store it with KVS (or any other way) and read it in B4XPage_Created of B4XMainPage.
 
Upvote 0
Top