Discover how to handle push notifications sent from Firebase Cloud Messaging (FCM) for Android and iOS

UPDATE: For anyone who has downloaded the demo prior to May 17th, 2017, there have been 2 changes: A UseSandbox property has been added to TPushClient, that determines whether the registration is for test apps, and activation of the service connection now happens in a separate thread, to account for threading changes in Tokyo.

A few months ago, I started writing some code that was aimed at being the basis for a job for a client (and may still end up that way), and because I also needed to handle push notifications in my own applications.

At first, it was going to use AWS (Amazon Web Services), via their Simple Notification Service (SNS) as a provider because it is a much lower cost solution than for example Kinvey, and other services, plus it supports a whole bunch of messaging services. As I dug deeper into how to connect with AWS and SNS, I saw it becoming fairly complicated, and as I was doing research about it, I stumbled across the fact that Firebase Cloud Messaging (FCM) (which used to be called Google Cloud Messaging), could handle both Android and iOS.

I started working on a solution, however it was pushed down my list of to-do’s, until I came across Jordi Corbilla’s excellent article on how to receive push notifications in Android. As I wanted to also handle receiving them on iOS, I started looking into how to achieve it. I wrote some code to do the process of registering an iOS device token and receive the FCM token back, however again it was shelved due to working on other tasks.

A post from Steven Chesser in the Embarcadero forums prompted me to take another look at it, and I finally came up with a standalone solution that works for both Android and iOS. I had always planned to make this part of the code public, so the code including a demo is at the PushClient project on Github. The demo and code was built with Delphi 10.2 Tokyo, however it should also work with XE8 and 10.1 Seattle.

Here’s a brief overview of how it works:

  • TPushClient (in the DW.PushClient unit) is responsible for creating an instance of the appropriate TPushService for the platform (either Android or iOS), and the TServiceConnection, which receives the messages.
  • When the device token becomes available,  if the platform is iOS, an instance of TRegisterFCM is created, and a request to register the iOS device token is sent asynchronously to FCM. When the FCM token comes back, the OnChange event of the TPushClient is fired, and you know you now have a valid FCM token.

You can send the FCM token to your back end (if you have one) so that you can target specific devices, however you don’t need to do keep track of them if you’re just interested in sending a message to all devices that have your app installed.

You can use the FCM console to send test messages, however you may want to have a “back end” service that does this, especially if you want to target specific devices or groups of devices. I’ll cover this in a later article.

Please read the above mentioned article from Jordi Corbilla about how to set up Firebase Cloud Messaging for your application.