Android Question Strange behavior with FCM service

Jaume Guillem

Member
Licensed User
Longtime User
I encounter a strange behavior with FCM.
The system works perfectly. I receive messages with the application started and with the application stopped.
But after making modifications to the service (FirebaseMessaging), with the app started the new code is executed well with the modifications, but with the app stopped or in backgroung the old code is executed.

I tried uninstalling, restarting the phone, re-sending after a tool-> clean project, and always happens the same.

Any idea on how to force the upgrade of the service code?
(I'm using the latest version of B4A, use macros in manifest, etc)

Thank you for help
 

KMatle

Expert
Licensed User
Longtime User
but with the app stopped or in backgroung the old code is executed

I don't really believe that. Did you check it with the debugger? (yes works, too). Can you show us the old AND new code. Maybe this behaviour is normal depending on the type of message you send (notification, data or both).

From the Google documentation @ https://firebase.google.com/docs/cloud-messaging/android/receive

1.JPG
 
Upvote 0

Jaume Guillem

Member
Licensed User
Longtime User
This is the OLD code

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
Dim Body As String=Message.GetData.Get("body")
Dim Title As String=Message.GetData.Get("title")

       Dim n As Notification
       n.Initialize
       Dim Lid As Int=Rnd(1,200)
       n=CreateNotification(Title, Body, "logo", Main, True, True)
   
       n.Notify(Lid)
End Sub

and this is the NEW

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
Dim Body As String=Message.GetData.Get("body")
Dim Title As String=Message.GetData.Get("title")

   Title=Title.SubString(10)

            Dim n As Notification
         n.Initialize
       Dim Lid As Int=Rnd(1,200)
       n=CreateNotification(Title, Body, "logo", Main, True, True)
   
         n.Notify(Lid)
End Sub

The only difference is
Title=Title.SubString(10)

This is because I use a header, and I don't want this header displayed in the notification.

When the app is active, the result is

Title of the notification
text of the notification

And when is the background the result is

1234567890Title of the notification
text of the notification

The message sent is of course Title= "1234567890Title of the notification" and Body="text of the notification"

And the result is exactñy the same with android 4.4.2 and android 7
 
Last edited:
Upvote 0

Marcus Araujo

Member
Licensed User
Longtime User
From my app, I noticed that when you send a notification and the app is not in foreground, the android system will display exactly in the way you sent (title and body), without calling MessageReceived. I believe this is the reason you think the old code is being executed, when in reality it is not being executed at all. Try logging in the messageReceived function to confirm that.

Edit: as KMatle shows in the chart, this happens when you send the notification as 'notification'. If you send as 'data', then the function shall be called.
 
Upvote 0

Jaume Guillem

Member
Licensed User
Longtime User
Thanks Marcus and KMatle. Probably this is the reason, because loggin, only appears the text logged when in foreground. It means that the function is not called.
I will try to send as data, and I'll report the results

Thank you
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Depending on the type of message Android will generate a notification on it's own. Then it displays the whole text of both (title & body) which here seems that the "old" version is running. I only use data messages only because I first did not know the dependencies and ran into similar situations.

With data messages you can do anything (nothing is done automatically). I use lists and maps (encrypted so Google can't read it), throw notifications or just handle the message.
 
Upvote 0

Jaume Guillem

Member
Licensed User
Longtime User
Thank you Erel. Of course, the tutorial is very clear and the multiple questions in the same way too.
But I already use B4J.

The problem has been simply my mistake interpreting this.
Trying to send a single notification for both platforms (Android and IOS) I've tried this code:

B4X:
Sub SendNotification(title as string,body As String,topic as string)
Dim API_KEY As String = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

   Dim Job As HttpJob
   Job.Initialize("fcm", Me)
   Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
   Dim data As Map = CreateMap("title": Title, "body": Body)

       ''''If Topic.StartsWith("ios_") Then
           Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
           m.Put("notification", iosalert)
           m.Put("priority", 10)
       ''''End If

   m.Put("data": data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub
and has been working perfectly for a long time, sending notifications to both platforms sending once, and they are always received well,

until I tried to send data, I found the problem described.


Sending correctly one message for each platform, everything works perfectly in all cases

Thank you very much to everyone. In this forum there is always a solution to all the problems.
 
Upvote 0
Top