As per the subject, has anyone had any luck getting an app to receive FCM notifications directly in an Android service whilst the app itself isn’t running?
I’ve spent quite a bit of time searching, but haven’t found anything to do what I want. The closest that I’ve found is the FCMRebooted demo by Dave Nottage (thanks Dave!) here:
With this demo, I can start the app and then swipe it away so that it’s no longer active and still have messages posted to the system log indicating that the sample service is receiving “data” FCM messages. The issues with this is that the service appears to be created fresh each time, so doesn’t have the ability to retain any state information. I’m guessing that I could set up a separate service that does run all the time and send messages to it with a broadcast intent - I haven’t spent much time looking at that yet though.
The bigger problem with this demo is that it always shows a notification to the user - I’m wanting the notifications to be invisible to the user normally - if the app does need to get the attention of the user, I’m planning for it to go the whole hog like an incoming phone call - screen on, vibrating and playing sound. I’m guessing that the notifications that are popping up are a feature of the Kastri JAR files that the demo is using to route the FCM messages to the service - I can’t see any way to modify those files myself.
Coming from a different angle, I’ve followed the instructions on the Embarcadero page here:
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Firebase_Android_Support
This allows me to silently receive FCM data messages when my app is active and also when it’s running in the background, however, if the app is swiped closed, it no longer receives any notifications.
Trying to move the code into a foreground service so that it survives the app being swipe closed hasn’t been a success: FMX.PushNotification.Android pulls in other FMX units which then means that the service doesn’t start properly since you can’t use FMX units in Android services. I have successfully modified FMX.PushNotification.Android to remove the dependencies on FMX units without breaking it - the modified unit still works in the main app. Trying to include it in the Android service though just leads to no messages being received - it does get the OnServiceConnectionChange event fired and it gets the same token as the main app, but still no dice. Interestingly, if I leave my NoFMX.PushNotification.Android unit in the uses clause of my service, the main app is also no longer able to receive notifications as well.
I’ve contemplated modifying the AndroidManifest.xml file to include an Intent Filter for my service as well as and/or in place of the intent filter that Delphi is adding in, but my expectation is that it’s not going to work still due to the way that FCM messages are routed through the proxy service that it appears is being silently included.
Thanks in advance to anyone that can help with this.