Android Question Firebase Notifications Problem

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

It's been a long time since I started to use Firebase Notifications in my apps. I noticed recently that if the app is in background the notifications are arriving BUT the code in the event fm_MessageArrived isn't being ran. Only IF the app is onscreen the code runs!

I really don't now since which Android version this changed but tested in and Android 9 and sdk 30 and it's really not running. It's a big problem because if this is the new Android behavior (in IOS it's the default behavior), this means that run jobs in background called by push notifications isn't possible anymore.
Something has changed but I didn't figure out how to fix it. Does anybody have the same problem?

Thanks !
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
How do you know the message is arriving when you say the code in fm_messagearrived isn´t running? fm_messagearrived is the place you get noticed about a new message.

What are you doing in fm_messagearrived?
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
How do ou know the message is arriving when you say the code in fm_messagearrived isn´t running?
What are you doing in fm_messagearrived?
hello @DonManfred .
Simple: I can see the notification on the device's notifications... the notification alert plays... but the code doesn't run (meaning that the behavior changed to the same of IOS).
I added a log in the messageArrived event... see the code:
fm_messageArrived:
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"), Main)
'    n.Notify(1)

    Dim title As String = Message.GetData.get("title")
    Dim body As String = Message.GetData.get("body")
   
    Select title
        Case "NEWORDER"
            If IsPaused(pedidos) = False Then
                CallSubDelayed(pedidos,"Activity_Resume")          
                mp.Initialize
                mp.Load(File.DirAssets,"orderArrived.wav")
                mp.Play
            Else
'                'Notify insistent
'                Dim n As NB6
'                n.Initialize("newOrder","Pedido Chegando","HIGH")
'                n.SmallIcon(LoadBitmap(File.DirAssets,"golunchrestaurantesIco-48x48.png"))
'                n.AutoCancel(True)
'                n.OnlyAlertOnce(False)
'                n.Build("Novo Pedido","Chegou mais um pedido!!!","",pedidos).Notify(1)
                CallSubDelayed(newOrder,"orderUpdate")
            End If
           
        Case "ORDERSTATUSCHANGE"
            If IsPaused(pedidos) = False Then
                CallSubDelayed(pedidos,"Activity_Resume")
            End If
           
            If IsPaused(orderDetail) = False Then
                Dim orderId As Long = body
                If orderDetail.orderId = orderId Then
                    refreshDetail(orderId)
                End If
            End If
       
    End Select
   
End Sub

This code runs perfectly when the app is on screen (I can see the log), but doesn't run when is in background (log isn't showed). But the notification is showed in both cases. Detail: the notification content is the content generated at server side (app didn't created the content - if you already developed Firebase for IOS, a data content is added to the server side because the IOS Device doesn't run any code if the app is closed).

That's it. Detail: this code ran perfectly about 4-6 months ago - I noticed this issue when developing an update (and after some customers sayng that the very important notifications - where I added a code to play a continuous sound in background weren't running anymore - only a default high priority notification was being generated when the app was in back..)
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
How do you know the message is arriving when you say the code in fm_messagearrived isn´t running? fm_messagearrived is the place you get noticed about a new message.

What are you doing in fm_messagearrived?
Found!

For the forum, could help:

Some time ago I added IOS support at server side for Firebase Notifications . At that time, everything was running well. I was sending the same data to IOS or Android devices (Android was ignoring the extra data required by IOS). But now, This:


Pay attention to this detail in the answers:
"Remove notification field completely from your server request. Send only data and handle it in onMessageReceived() otherwise your onMessageReceived() will not be triggered when app is in background or killed."

That's it! Now, I'm thinking what can I do to make server send the correct message depending the target device... if I simply remove the notification field at server side, IOS notification isn't triggered. If I don't, Android code in background isn't triggered...

A good workaround could be send two notifications from server. One with extra data and other without... IOS will react to one... And Android can manage with code... let's see.

That's it. Thanks @DonManfred
 
Upvote 0
Top