Android Question FCM not triggering MessageArrived event when device asleep

Ian Garton

Member
Licensed User
Longtime User
Hello

I'm now using B4A v8, and have the FirebaseMessaging service as per this thread --
https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

When the device screen is on, the MessageArrived event is firing and the associated code running correctly.
However when the device screen is off, the device receives a notification sound and icon, but the MessageArrived event is not firing.

My manifest is targeting the following --
<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="26"/>

Any ideas?
 

DonManfred

Expert
Licensed User
Longtime User
Update to b4a 8
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the device receives a notification sound and icon, but the MessageArrived event is not firing
isn´t it the messagearrived sub which creates the Notification? So; if you get a notification then the sub must be run as it is this sub who created the notification.

B4X:
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)
End Sub
 
Upvote 0

Ian Garton

Member
Licensed User
Longtime User
No, the FCM message contains both a data string and a notification object. I can tell from adding of log statements that the event is not firing when the device screen is off.
Works perfectly when the screen is on.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
and a notification object
It only contains the info. The Notification is made by your code.

the device receives a notification sound and icon
Where does the notification and sound come from if it is not your sub???
Firebase itself does not create a Notification! They just sends the Pushnotification and the rest is in your app.
 
Upvote 0

Ian Garton

Member
Licensed User
Longtime User
My issue isn't with the notification, it's with the fact that when the screen is off on the device the Firebase service's MessageArrived event doesn't trigger. However it does when the device's screen is on.
The first line of this event is Log(Message) so I can see it's not firing from the logcat.
 
Upvote 0

Ian Garton

Member
Licensed User
Longtime User
Ok, I seem to have found what's happening.
If I have my sending payload as
B4X:
{"notification": {
        "title": "Notification Title",
        "text": "Notification Text"},
    "data": {
        "body" : "body data"},
        "to" : "cAMVL25vxrE:adfadsfasdfVROjv4HeCFojxbxWvJudlzgRYMh8KyuR0KU7ySQu4rWqIYqTzkdneInkCOnorLsll_Wu9HEsjXvr2s54ACXGkRcCXTW3Fuu7B5LKXe2NxD5K6bzygK5n"
}
then the event fires whilst the screen is on, but the notification fires when the screen is off.

If I remove the notification portion of the payload the event fires in both instances.

B4X:
    "data": {
        "body" : "body data"},
        "to" : "cAMVL25vxrE:APA91bFqOWtC_L3mYMYl4fQMVROjv4HeCFojxbxWvJudlzgRYMh8KyuR0KU7ySQu4rWqIYqTzkdneInkCOnorLsll_Wu9HEsjafafdasdfasdfsadf3Fuu7B5LKXe2NxD5K6bzygK5n"
}
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
See Google's documentation of the different types of messages such as data/foreground/background. FCM handles them different. I always use data messages like you now. Then you have to throw notifications on your own but it's more flexible.
 
Upvote 0
Top