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:
227

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: 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\

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:

AudioToolbox
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": "<yourtoken>"
    "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-05-17T20:13:23+00:00 May 10, 2017 10:22 pm|Code tips, General tips, Using APIs|2 Comments

About the Author:

2 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?

Leave a Reply

Show Buttons
Hide Buttons