Home/Code tips, General tips, Using APIs/Adding Firebase Cloud Messaging to your mobile apps – part 2

Adding Firebase Cloud Messaging to your mobile apps – part 2

Views:
290

This is Part 2 of a 2-part article on incorporating support for FCM in your Delphi apps for Android and iOS, and this part covers implementation on iOS.

UPDATE 3: A change in the Firebase SDK (from version 4) has meant that the demo will not compile without a couple of changes, namely to include the FirebaseNanoPB framework in the search paths, and a corresponding change to the DW.iOSapi.Firebase unit. See step 2 for the details regarding the search path, and go to the source to download the changed unit.

UPDATE 2: Thanks to Steven Chesser who found this issue, and to Dmitry Arefiev for coming up with a solution:

If you’re using FireDAC with SQLite in your app, you’ll likely find that the app crashes on startup. It appears that FireDAC binds to SQLite static libraries that are incompatible with FCM. To alleviate this issue, please follow these steps:

  1. Make a copy of FireDAC.inc from the C:\Program Files (x86)\Embarcadero\Studio\xx.0\source\data\FireDAC folder, and put it in your project folder (where xx is the number for your Delphi install e.g. 18 for Delphi 10.1 Berlin)
  2. Modify FireDAC.inc to change the line:
    {.$UNDEF FireDAC_SQLITE_STATIC}
    Remove the “.” after the “{” so that it becomes:
    {$UNDEF FireDAC_SQLITE_STATIC}
  3. In Project Options, select: All configurations – iOS Device- 64 bit platform, and add the following to the search path:
    $(BDS)\source\data\FireDAC
  4. Clean and Build the project (as opposed to Compile), run it, and all should be OK

UPDATE 1: Thanks again to Stephane Vanderclock, notifications on iOS will now appear in the notification center, even when the app is in the foreground! To take advantage of this, check out the latest version of the demo from the Kastri Free project.

If you haven’t done so already, please read at least step 1 of Part 1, because it has important information about how to retrieve the .plist file that’s required for your iOS app, and about the starter project.

This article assumes that you have a certificate, app id and provisioning profile set up already, and added to your Mac. If you do not, please refer to this article.

Let’s start!

1. Download the Firebase SDK from the Firebase site

Go to the Firebase iOS setup page, navigate to the “Integrate Without Cocoa Pods” section (since we’re not using Cocoa Pods) and click the link in step 1 to download the SDK. Extract it somewhere appropriate that’s common to your applications. Inside, you’ll notice a a bunch of folders, and inside each folder is one or more “framework” folders (they have an extension of “.framework”). Inside each of those, in the root, is a file with the same name, with no extension. These are the binaries that your app needs in order to use the Firebase SDK.

2. Add paths to the framework files in your Delphi project

The easiest way to make it so that the compiler can find the files is to add a path for each of the necessary frameworks. In the Project Options for your Delphi project, select Delphi Compiler in the tree, and select All configurations – iOS Device – 64 bit platform in the Target combo. In the Search Path option, you’ll need to add a path for each of the following (where <firebase> is the root of the Firebase SDK that you extracted):

<firebase>\AdMob\GoogleMobileAds.framework\
<firebase>\Analytics\FirebaseCore.framework\
<firebase>\Analytics\FirebaseAnalytics.framework\
<firebase>\Analytics\FirebaseInstanceID.framework\
<firebase>\Analytics\GoogleToolboxForMac.framework\
<firebase>\Messaging\FirebaseMessaging.framework\
<firebase>\Messaging\Protobuf.framework\
<firebase>\RemoteConfig\FirebaseRemoteConfig.framework\
<firebase>\Analytics\FirebaseNanoPB.framework\     (this is a recent addition, to support Firebase SDK 4, and does not appear in the image below)

e.g:

While we’re in the Project Options, you’ll also need to add a parameter to the Linker options. Under Delphi Compiler, select Linking, and add -ObjC to the “Options passed to the LD linker” option (as per part 3 of the SDK requirements for apps not using Cocoa Pods on the Firebase iOS setup page):

You’ll need to repeat the above for iOS Device – 32 bit platform when you’re ready to deploy to the App Store or Test Flight, unless Apple has removed the 32-bit requirement by then.

Go to the Version Info section of the Project Options and modify the CFBundleIdentifier field so that it matches the identifier for your App ID on the Apple Developer site:

Then go to the Provisioning section, just to make sure the certificate and provisioning profiles have been found, and click OK.

3. Add the required frameworks from the iOS SDK to the Delphi SDK Manager

If you’re not already familiar with how to add other frameworks to the iOS SDK, you can refer to this article. You’ll need to add each of the following frameworks:

AdSupport
AudioToolbox
AVKit
CoreAudio
CoreMIDI
MediaToolbox
Metal
SystemConfiguration
UserNotifications

Note that the case of the name is very important! (eg it’s CoreMIDI, not CoreMidi). Remember to click Update Local File Cache when you are done adding those, and remember to click OK.

4. Add GoogleService-info.plist to the deployment

In Delphi, click Project, Deployment and select All configurations – iOS Device – 64 bit platform in the combo. Click the button with the white rectangle and green “+” on it to add, then select your GoogleService-info.plist file and click Open, e.g:

Now: would you believe that is it? Other than compiling and running your project, that is. While Monitor is good for watching log messages on Android, on iOS I find iOSConsole to be a decent equivalent on iOS, and it doesn’t require you to start Xcode. If you download that (onto your Mac), install and run it, you’ll be able to see the log messages for the starter project. Put the text “FCMStarter” (without the quotes) in the filter box of iOSConsole.

Run the app, and the first time you do, you’ll be prompted as to whether you would like the app to receive notifications. Be sure to tap “Allow”! You should also see the token appear in the upper memo. The token will also appear in the iOSConsole window:

Just like in Android’s Monitor app, you can copy the lines in iOSConsole, and extract the token from the copied text.

For iOS, FCM messages need to be of the notification format, e.g:

{
  "to": "&amp;amp;amp;lt;yourtoken&amp;amp;amp;gt;"
    "notification" : {
    "body" : "Congratulations, you received a message",
    "title" : "Firebase Cloud Messaging rules!",
    "badge": "0"
    }
}

Where <yourtoken> is the one you retrieved from the log.

Please refer to the last step of the previous article where I mention how to use Hurl to send test messages. If you’re using the starter project, you should check that notifications appear in the memo on the main form when the app is running, and for all apps using this framework, notifications will appear in the notification center when the app is in the background, or not running at all.

That’s all on this subject for the moment. There may be follow-up articles as to how to go about using the SubscribeToTopic and UnsubscribeFromTopic methods, and further developments going forward.

By | 2017-06-25T13:37:30+00:00 May 10, 2017 10:22 pm|Code tips, General tips, Using APIs|7 Comments

About the Author:

7 Comments

  1. Vsevolod May 19, 2017 at 3:48 pm - Reply

    Hi. What are the differences in the settings if the project is for С++ Builder? Thanks.

    • admin May 21, 2017 at 8:08 am - Reply

      I don’t use C++Builder, so I’m unaware of what the differences might be. Are you having a particular issue?

  2. Marco June 17, 2017 at 10:35 am - Reply

    Hello Dave!

    Congratulations for this post. I’ve been waiting a long time for this solution. Thank you very mutch!

    I have no issue on Android deployment, its everything ok.

    For IOS, I downloaded Firebase SDK exactly is specified in step 1 (Firebase iOS setup page-Integrate Without Cocoa Pods). After unzip it, I realized that folder “\Messaging\Protobuf.framework\” and it framework doesn’t exist. So, do you have, please, another url to download the same version of Firebase SDK you used in this project?
    I got a second error message too: “[DCC Error] E2597 ld: framework not found AdSupport for architecture arm64”, I don’t know if this one is related to the Firebase SDK version

    Your help is appreciated

    Best regards,

    Marco

    • Dave June 17, 2017 at 11:01 am - Reply

      Hi Marco,

      It looks like I’m going to need to update the code and the article, as there doesn’t appear to be a way of downloading earlier versions of the SDK, and it’s probably a good idea to update it anyway.

      Stay tuned

    • Dave June 17, 2017 at 12:05 pm - Reply

      I’ve now updated the article, and one of the units to support Firebase SDK 4 (and above)

  3. Marcelo June 22, 2017 at 3:26 pm - Reply

    Hello Dave,

    First of all, congratulations on the great work!

    After following all the steps and trying to compile, I get the same error message as Marco: ld: framework not found AdSupport for architecture arm64

    I can’t find the error. Any help is very much appreciated.

    Thank you!

  4. Marcelo June 22, 2017 at 4:00 pm - Reply

    Hello Dave,

    Thank you for this great piece of work!

    At first I had some trouble to get it running, but now I can receive remote notifications from Firebase perfectly!

    Here’s what I had to do:

    1. Add frameworks “AdSupport” and “AVKit”, and Update Local File Cache
    2. Edit DW.iOSapi.Firebase, add “procedure setDelegate(delegate: Pointer); cdecl;” on line 126. It seems that setRemoteMessageDelegate is deprecated and causes a crash if used.
    3. Edit DW.Firebase.Messaging.iOS, commenting line 179 (“TFIRMessaging.Wrap(TFIRMessaging.OCClass.messaging).setRemoteMessageDelegate(FFIRMessagingDelegate.GetObjectID);”)
    4. Edit DW.Firebase.Messaging.iOS, adding line “TFIRMessaging.Wrap(TFIRMessaging.OCClass.messaging).setDelegate(FFIRMessagingDelegate.GetObjectID);”

    After that, remote notifications are received.

    Thank you very much and keep up the great work!

Leave a Reply

Show Buttons
Hide Buttons