Back in October last year, I wrote about integrating iOS support for FCM using Embarcadero’s implementation. In this article, I describe a revised demo that simplifies things and fixes a couple of issues.

STTP

The new demo can be found here. Please be aware that the ShowBannerIfForeground must be set to False if you don’t want a banner showing while the app is in the foreground, and that both data and notification elements must be present for Android push messages when the app is running, for everything to work as expected. See below for more details.

The new demo

The previous demo was basically just an expansion of example code presented by Embarcadero when their support for FCM (Android only) was introduced. The new demo encapsulates that code into its own unit, so all that needs to be done is to create an instance of the class (TPushNotifications) and assign a couple of event handlers.

The issues

Showing a banner when the app is not running

One of the main issues with the FCM implementation for Android is that a banner does not show when the application is not running, i.e. all that shows is the notification icon in the status bar.

The banner shows only if the Importance of the channel is set to High, so the startup code in TPushNotifications creates the channel, and sets the Importance to High. The channel id is extracted from the application metadata (in AndroidManifest.xml) using some code recently introduced into the Kastri Free library (DW.OSMetadata.XXX units).

Much larger issues

There are some extremely annoying quirks with FCM on Android, and I’m hoping I’m just doing something wrong and that someone can point it out, however from what I’ve discovered:

When an app is in the background (i.e. still running, but not in the foreground), in order for a notification to be received, the payload must contain a data element. However, if the app is not running at all, the payload must contain a notification element, otherwise no notification is presented.

In addition, when the app is in the foreground, no banner shows at all. To allow for this, I’ve updated the code to present a local notification, which will show when the ShowBannerIfForeground property of TPushNotifications is set to True. Note that this property applies to Android only, as it works on iOS.

Now all will work as expected, as long as messages sent to Android devices contain both a notification and data element (they can contain the same data).

Configuration

Remember to import your google-services.json file for Android:

..and to provide your Google-Services.info.plist for iOS (for the demo, it will go in the Resources folder)

Sending messages

I’m planning an article which covers creating a server which includes sending messages, however for now for testing you could use the standalone testing tool: PushIt.

As above, messages to Android devices should contain both a notification and a data element for everything to work correctly. Here’s an example payload:

[sourcecode language=”javascript”] {
"to":"fDYt7NwXGyM:APA91bE_5ACVjyRubh3-s_kRD6Ig83LX8VKcsEFCxIfDemw49AIHudDGVQwnJ5r1UMlPBc2A2P6yrEZ6fbBsejrHaX5vQQ-RdpUbRiRk1UURknTDrJfMIMqkTZGd0mstxLWsMaI-pWjl",
"notification":{
"android_channel_id":"EMBTFCMv2",
"title":"Testing.. 1..2..3..",
"body":"FCM still rules!"
},
"data":{
"android_channel_id":"EMBTFCMv2",
"title":"Testing.. 1..2..3..",
"body":"FCM still rules!"
}
}[/sourcecode]

For iOS, there should be a notification element only.

Conclusion

I sincerely hope that I’ve missed something in regards to what needs to be in the payload to make things work as expected on Android, so if anyone can shed some light on the problem please let me know, however I do know that using the methods above works.