A little over a year ago, I posted a two part article about integrating Firebase Cloud Messaging in your Delphi mobile apps. This time, it’s a single part article because much of the work is done for you, or is simplified.
The code in this article was built and tested using an install of the latest Delphi 10.2.3 (build 25.0.31059.3231), and at least for iOS, will require specifically this version. For Android you may be able to make it work in Delphi 10.1 Berlin or earlier.
TL;DR: The demo can be found here, bearing in mind that it requires other parts of KastriFree, and requires you to do a little configuration (see the Configuration section in this article)
Firebase SDK changes
There have been quite a number of releases since the original article, which used version 10.2.1 of the Firebase Android SDK, and version 4.3.0 of the Firebase iOS SDK. This article uses the latest released SDKs: the Firebase Messaging Android library is numbered 17.1.0 (libraries that it depends on have lower numbers) and the Firebase iOS SDK is at 5.5.0.
In the previous articles, it was up to the reader to download and extract the SDKs using the tools described in the articles. This time, I have uploaded the bare minimum libraries to the KastriFree project so that the demo project should just compile and run, after supplying a couple of files of your own.
Firebase Cloud Messaging (FCM) support changes
Due to changes in the SDKs, the supporting code has also changed. This means that the older demo will probably not compile, and will certainly not work using the older SDKs.
Support for later versions of iOS and Android
Some may have noticed that notifications (both local and remote) are not working as expected in later versions of iOS. In this solution, the device will display a push notification when the app is not running, when the app is in the background, and when in the foreground. This is because it makes use of the later APIs, including UNUserNotificationCenter which allows notifications to show in the foreground.
Similarly, if on Android you are targeting API 26 or greater (which is a requirement for Google Play apps), you may have noticed that both local and remote notifications do not show at all. This is due to the API 26 requirement of the use of notification channels. The FCM support in this solution makes use of these, so your notifications will show. This solution also makes use of TPermissionsRequester introduced in an article regarding targeting API 26 or greater, so that the necessary “dangerous” permissions are handled.
There is a known issue with linking to the Firebase iOS SDK when targeting some versions of the iOS SDK, namely iOS SDK 11.3 or later. It has also been reported that this is sometimes an issue with iOS 11.2 SDK, however it is not always the case. Hopefully this issue will be resolved in the next release of Delphi.
As per the introduction above, much of the work this time has already been done for you. However there are two important aspects that you will need to take care of before the demo will work. In part 1 of the original article, once you have set up your Firebase Console project (for each platform you require), you will need to download google-services.json from the Android section, and GoogleService-info.plist from the iOS section.
- For iOS:Save GoogleService-info.plist into the Resources folder of the project. You don’t need to add it to the Project Deployment because it has already been added.As per section 2 of the second article, you will need to modify the CFBundleIdentifier value:As per section 3, add the following frameworks to the iOS SDK:
- For Android:As per section 3 of the first article, you will need to modify the package name value:Create a copy of strings.template.xml from the KastriFree project, open it in an editor, then insert values for each of the entries that correspond to the values from google-services.json:
and save the modified strings.template.xml as strings.xml, to the Resources folder of the demo.
You won’t need to do this for the demo, however for other projects you will need to add the required jars, and disable conflicting jars, as per this image:
As per the intro, the demo is here, and remember that it relies on files from KastriFree.