Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

Delphi 11 – Firebase Cloud Messaging ( FCM ) – iOS app Push Notification

Digital solution partner

Delphi 11 – Firebase Cloud Messaging ( FCM ) – iOS app Push Notification

Goals:

  • Integrate Firebase push notifications (FCM) into an app built with Delphi for iOS without the help of third-party libraries.
  • Having the same base code in the application and in the servers for managing pushes in Delphi the same as that used for Android.

 

Test environment used:

  • VM Delphi 11 Alexandria Ent. Edition on Windows 10 pro 64GB Ram
  • Xcode 13.2.1 
  • Mac OS Menterey 12.0.1
  • iOS sdk iPhoneOS 14.5
  • Device test
    • iPad 10.2 myl92ty/a os. ver 14.3
    • iPhone 12 mini

Prerequisites:

  • IOS application operational on your devices in development
  • Firebase account and created project

Steps needed to associate notifications

  1. Configure the distribution of the app on the developer.apple.com portal and on https://appstoreconnect.apple.com/apps/ <appId>
  2. Add the management code on the Delphi application and import the firebase sdk for iOS
  3. Configure the project on firebase https://console.firebase.google.com/project/
  4. Download the “GoogleService-Info.plist” file from firebase, copy it to the project directory and insert it in the distribution list (deploy)
  5. Sending a test push message from the firebase console or your server

Step 1: Configure the distribution of the app on the developer.apple.com portal and on https://appstoreconnect.apple.com/apps/ <appId>

  • (apple portal) Configure the distribution of the app on the apple developer portal to receive pushes
    • create a certificate for push notifications
    • enable “Push Notifications” in the “Identifier” section and associate a certificate for the “Production SSL Certificate” push (mac keystore -> Keychain Access -> Certificate Assistant -> Request a certificate from a certification authority)
    • always in the “keys” section of your developers panel Create a key for pushes
    • (if not already done) create an app on “app Store Connect” (the “sku” code is your internal code (write us what you want, just remember it))
    • publish the app on the App Store Connect even without push support using the “Transporter” program

Step 2: Add the management code on the Delphi application and import the firebase sdk for iOS

  • (delphi) Download the FCM SDK package from getit and unzip it in the proposed directory or choose a custom path
  • (delphi) Create an “environment variable” in which to define the base path of Friebase SDK for iOS (in my case C: \ Users \ <myusername> \ Documents \ Embarcadero \ Studio \ 22.0 \ CatalogRepository \ FirebaseSDKforiOS- 6.28 \ Firebase) and which I called “Firebase_6_28
  • (delphi) Change the search path in the project options ( project –> options –> Delphi compiler –> search Path :

    •  
  • (delphi) directive “-ObjC” to linker LD (project -> options -> Delphi Compiler -> Linking) to allow to include SDK methods
  • (delphi) copy the Delphi “iOSapi.FirebaseCommon.pas” file to your project folder ( C:\Program Files (x86)\Embarcadero\Studio\22.0\source\rtl\ios\iOSapi.FirebaseCommon.pas –> e:\progetti\myApp ) edit the file and write in it:

    •  
  • (delphi) copy the Delphi “iOSapi.FirebaseMessaging.pas” file to your project folder ( C:\Program Files (x86)\Embarcadero\Studio\22.0\source\rtl\ios\iOSapi.FirebaseMessaging.pas–> e:\progetti\myApp )

  • Delete from the files the writing “{$ IFNDEF IOS32} framework {$ ENDIF}” (directive to the compiler) from the two files just mentioned
  • Include the Embarcadero libraries for managing pushes via FCM in the main project form

  • We define in the form the methods of managing the notification reception and the connection change, we also use the onclick event “btnInitializePushClick” of a button “btnInitializePush” to perform the initialization of the notification management. The interface is managed in this demo by a “memoLog” Tmemo
  • We implement the method that manages the receipt of notifications
  • Push initiation via the button “click” event:

  • Step 3: Configure the project on firebase:

  • Add the app to the project by clicking on “Add App” and download the GoggleService-Info.plist file to your Delphi project directory.

  • In the settings section you have to upload the APN certificate that you previously generated on the apple portal in the “Keys” section and upload it to:

 

 

At this point, compile the application and run it on your device.

Inside memoLog you will find a JSON containing the deviceID, you have to copy it to test sending the first message from firebase.

 

Step 5) Send a test push message from the firebase console or your server

 

Copy the server key and perform a rest call as follows:

 

The JSON must also contain the “notification” object if you want the notification to arrive even when your app is not active:

 

that’s all