Android Question FCM messages when app is not running?

Computersmith64

Well-Known Member
Licensed User
Longtime User
It's not clear to me (either from the official FCM documentation, or Erel's tutorial) what is supposed to happen to a FCM message sent to a topic when an app that subscribes to that topic isn't running.

I understand what happens when the FirebaseMessaging service is running & the app is either in the foreground or background & my testing shows that my app is handling that fine, however when I force close the app, then send a message from the app running on another device, what is supposed to happen? My testing has shown that the message is never delivered - even after the app is restarted. If I then send another message from the other device, it gets delivered, but the original message is never received.

Is this the expected behavior, or is there something I'm not doing on the stopped/restarted app?

- Colin.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It's not clear to me (either from the official FCM documentation, or Erel's tutorial) what is supposed to happen to a FCM message sent to a topic when an app that subscribes to that topic isn't running.
A notification should appear.

The relevant service will start and the notification will be displayed.

Make sure to send the messages with the B4J code and not Firebase console.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
A notification should appear.

The relevant service will start and the notification will be displayed.

Make sure to send the messages with the B4J code and not Firebase console.

Actually I'm sending the messages from a B4A app. There doesn't seem to be an issue receiving them when the FirebaseMessaging service is running on the "client" app, however if the service isn't running it's not being restarted, nor am I seeing a notification. I'll investigate some more & see if I can figure out what's going on...

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Test it with the B4J code. Make sure not to kill the app with a swipe and to run it in release mode.
I am using exactly that code, except in a B4A app (& without the MessageLoop obviously).

According to the documentation, messages that can't be delivered immediately (due to the device being off-line, or whatever) have a time_to_live default of 4 weeks, so should be delivered when the app is back online - but I'm not seeing this happen.

So in summary:

1) FCM works fine as long as the app/FCM service is running on the receiving device.
2) If the app/FCM service isn't running on the receiving device, the message is never delivered. There is no notification & even after the app is started, that message is lost forever - ie: subsequent messages sent after the app was restarted are delivered fine.

To make things even more confusing, the collapsible / non-collapsible behavior seems to be the opposite of what the FCM documentation states. According to the documentation, all messages (except for notification messages) are non-collapsible by default - however I am seeing the opposite. If I send a message using your B4J code, then send another one, the first notification is overwritten by the second. However, if I include the "notification" key in the message construction & then send 2 messages, both notifications appear.

Scratch that - I didn't notice that the call to Notification.Notify(1) was using the same index "1" every time. That was causing the notification to be updated instead of a new one appearing.

I'm confused!

- Colin.
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I ran a test where I have the receiving app force stopped on one device & I send a FCM message from another device to a topic that the stopped app is subscribed to. The app is installed in release mode on the receiving app & the device is connected via usb so that I can see the logs in the B4A log window.

Message sent - no notification on receiver device, nor any sign that the FirebaseMessaging service has started. When I looked in the unfiltered logs I found this:

broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.airlinemates.followme (has extras) }

This looks to me like the it tried to start the service/app, but for some reason it failed. Any ideas?

- Colin.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I have the receiving app force stopped
if you force stop the app the receiver will not start unless you manually start your app again.

Do not swipe a app from the recent app list.
Doing this will force android to handle the app as stop receiving messages.

Start you app. Send a message from another. wait till the one arrives.
Close your app "in the NORMAL way" (back-key)....

Send another message. Does your app start again to get the message?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
if you force stop the app the receiver will not start unless you manually start your app again.

Do not swipe a app from the recent app list.
Doing this will force android to handle the app as stop receiving messages.

Start you app. Send a message from another. wait till the one arrives.
Close your app "in the NORMAL way" (back-key)....

Send another message. Does your app start again to get the message?

Heh! Yes - it all worked as expected when I didn't force stop the app, however I was trying to figure out what would happen to messages if the app wasn't running (like if the device was turned off or out of service). I was assuming that force stopping the app was effectively the same thing as turning the device off & then back on...

When I did a test by turning the device off & back on, I found that when it restarted I still didn't get any messages sent while it was off (because the app didn't restart when the device did) - BUT, when I started the app & waited a little while, the missing messages came through.

So lesson learned - force stopping an FCM app isn't the same as stopping it by turning the device off. :)

- Colin.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I was trying to figure out what would happen to messages if the app wasn't running (like if the device was turned off or out of service).
If not swiped (see above) the app will receive the message probably later when it is on again. If the device is off for a couple of weeks then the message will not arrive i guess.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
If not swiped (see above) the app will receive the message probably later when it is on again. If the device is off for a couple of weeks then the message will not arrive i guess.

Yeah - the default is 4 weeks.

- Colin.
 
Upvote 0

Alpandino

Member
Licensed User
If not swiped (see above) the app will receive the message probably later when it is on again. If the device is off for a couple of weeks then the message will not arrive i guess.
I can tell my experience: in my messaging app I always receive messages, also when I swipe off my app. I only have problem in some devices (for example Huawey): in some models the energy saving functions stop my app and inhibit any service to restart. But if I put my app in the "saving list" all works well.

Bye
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I can tell my experience: in my messaging app I always receive messages, also when I swipe off my app. I only have problem in some devices (for example Huawey): in some models the energy saving functions stop my app and inhibit any service to restart. But if I put my app in the "saving list" all works well.

Yep, here, too. It's not relevant if an app is "stopped". Android will start the app again.

External proof: Swipe WhatsApp or Facebook Messenger...

It is important what kind of message you SEND. See here: https://firebase.google.com/docs/cloud-messaging/android/receive

PS: Funny thing Huwei forgot to add WhatsApp and Google Calendar to their whitelist in previous versions so even the calendar did not update and WhatsApp was closed and couldn't receive messages. This feature made me mad while testing my App using FCM on my Huawei P8 not knowing this at the start.
 
Upvote 0
Top