Android Question [SOLVED] Error receiving firebase notification

ermales

Member
Licensed User
I sent a notification from the firebase console if the application is closed there is no error but when the application is open I have the following error .
what should I do?

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
POL : 1
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (menu) Create, isFirst = true **
** Activity (menu) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Service (httputils2service) Start **
** Receiver (firebasemessaging) OnReceive **
** Service (firebasemessaging) Start **
Message arrived
Message data: {}
firebasemessaging_fm_messagearrived (java line: 155)
java.lang.RuntimeException: Cannot change properties after call to SetInfo. Initialize the notification again.
at anywheresoftware.b4a.objects.NotificationWrapper.getND(NotificationWrapper.java:92)
at anywheresoftware.b4a.objects.NotificationWrapper.setFlag(NotificationWrapper.java:156)
at anywheresoftware.b4a.objects.NotificationWrapper.setAutoCancel(NotificationWrapper.java:130)
at netlabel.bancodepreguntasantpremium.firebasemessaging._fm_messagearrived(firebasemessaging.java:155)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA$1.run(BA.java:335)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

this is the code, guided in this tutorial -> https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

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

Sub Process_Globals
    Private fm As FirebaseMessaging
   
End Sub

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

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
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)
    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"), Menu)
    n.AutoCancel=True
    n.Notify(1)
   
End Sub

Sub Service_Destroy

End Sub
 

josejad

Expert
Licensed User
Longtime User
Not sure if can be your problem, but I’ve read some place “SetInfo” must be the last command before call the notificatin.
Try to move it down “Autocancel”
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I sent a notification from the firebase console
Here the problem starts.
Send the Notifiction with the B4J App

Message data: {}
There is no data. Even if you use the Notication right (see answer from @José J. Aguilar too) it will be empty as there is no text and body inside the Message arrived.
 
Upvote 0

ermales

Member
Licensed User
the problem is solved when “SetInfo” to move it down “Auto Cancel”

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

Sub Process_Globals
    Private fm As FirebaseMessaging
    
End Sub

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

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
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)
    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"), Menu)
    n.AutoCancel=True
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Menu)
    n.Notify(1)
    
End Sub

Sub Service_Destroy

End Sub

but when the app is open the notification does not arrive, the null message arrives, what could it do?

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
POL : 1
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (menu) Create, isFirst = true **
** Activity (menu) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Service (httputils2service) Start **
** Activity (menu) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
POL : 1
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (menu) Create, isFirst = true **
** Activity (menu) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Service (httputils2service) Start **
** Receiver (firebasemessaging) OnReceive **
** Service (firebasemessaging) Start **
Message arrived
Message data: {}
** Activity (menu) Pause, UserClosed = false **
** Activity (menu) Resume **
** Activity (menu) Pause, UserClosed = false **
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but when the app is open the notification does not arrive, the null message arrives, what could it do?
New issue -> Create a new thread posting all relevant info. Message data: {} means the pushnotification you got does not contain the relevant data. Send the Notification with the B4J App and NOT with the console
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
It is possible to use the Firebase console.
It seems that B4X only picks up the custom data fields, so you need to place your data there.
the standard fields are ignored.
Using the B4J tool is much simpler.

Don't forget to set the topic also.

upload_2019-1-14_17-3-14.png
 
Upvote 0
Top