Firebase Cloud Messaging
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.
It is the default push notification platform for Android devices, but can also be used on iOS devices.
This section describes how to setup your AIR application to use Firebase Cloud Messaging with this extension.
This service is supported by the following variants of this extension:
- FCM
- base (i.e. the main extension)
- Android only: use the base variant if you plan on using APNS on iOS separately from FCM
Setup
You should firstly setup a Firebase project for your application. See the setup documentation to guide you through this process.
Install
The simplest way to install and manage your AIR native extensions and libraries is to use the AIR Package Manager (apm
). We highly recommend using apm
, as it will handle downloading all required dependencies and manage your application descriptor (Android manifest additions, iOS info additions etc).
However you can choose to install it manually, as you would have done in the past.
- APM
- Manual
Note: All of the commands below should be run in a terminal / command prompt in the root directory of your application, generally the level above your source directory.
If you don't have an APM project setup, expand the guide below to setup an APM project before installing the extension.
Setup APM
Install APM
If you haven't installed apm
follow the install guide on airsdk.dev.
Setup an APM project
You will need an APM project for your application.
There are many ways to do this and for more options see the APM documentation. Here we will just initialise a new empty project:
apm init
Check your github token
We use github to secure our extensions so you must have created a github personal access token and configured apm
to use it.
To do this create a token using this guide from github and then set it in your apm config using:
apm config set github_token ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXX
If you don't do this correctly you may find the install will fail.
Install the extension
Install the extension by running:
apm install com.distriqt.PushNotifications-FCM
This will download and install the extension, required assets, and all dependencies.
If you are using the base variant install
com.distriqt.PushNotifications
instead.
Once complete apm
will have created something like the following file structure:
.
|____ ane
| |____ com.distriqt.PushNotifications.FCM.ane # PushNotifications extension
| |____ [dependencies]
|____ apm_packages # cache directory - ignore
|____ project.apm # apm project file
- Add the
ane
directory to your IDE. See the tutorials located here on adding an extension to your IDE.
We suggest you use the locations directly in your builds (rather than copying the files elsewhere) so that updates with apm
will be pulled automatically into your build.
You must set a few pieces of configuration information for APNS. This will allow
apm
to automatically insert them into the correct position in your application descriptor. This includes:bundleSeedId
: Bundle Seed Id (or App ID Prefix) for your iOS applicationapsEnvironment
:production
for release builds anddevelopment
for development buildsgetTaskAllow
:false
for release builds andtrue
for development builds
You can call the following to step through the all the configuration values for this extension:
apm project config set com.distriqt.PushNotifications
We suggest creating a build type to allow you to easily switch between the production and development configurations. To do this run the following additional commands (assuming you setup the main configuration for production):
apm -b development project config set apsEnvironment development
apm -b development project config set getTaskAllow true
This will set the values of those properties to be different for the "development" build type.
The following guide is used to manually install the extension, download dependencies and update the application descriptor. We highly recommend installing extensions using apm
. Using apm
will automate the installation and automatically handle updates and dependencies along with greatly simplifying the application descriptor generation.
First step is always to add the extension to your development environment. Download the extension from the repository and then follow the tutorial located here to add the extension to your development environment.
To use this service add com.distriqt.PushNotifications.FCM.ane
variant of the extension to your project in place of com.distriqt.PushNotifications.ane
. You should only add one of the variants to your project. If you need to support multiple services please use the All Services variant.
All variants of the Push Notifications extension have the same extension id: com.distriqt.PushNotifications
so you should add this to your extensions list in your application descriptor:
<extensions>
...
<extensionID>com.distriqt.PushNotifications</extensionID>
...
</extensions>
Dependencies
Many of our extensions use some common libraries, for example, the Android Support libraries.
We have to separate these libraries into separate extensions in order to avoid multiple versions of the libraries being included in your application and causing packaging conflicts. This means that you need to include some additional extensions in your application along with the main extension file.
You will add these extensions as you do with any other extension, and you need to ensure it is packaged with your application.
Core
The Core extension is required by this extension. You must include this extension in your application.
This extension requires you call the init()
function at some point early in your application, generally at the same time as the initialisation of this extension. If you are using other extensions that also require the Core extension, you only need to initialise it once, before initialising the other extensions.
Core.init();
The Core extension doesn't provide any functionality in itself but provides support libraries and frameworks used by our extensions. It also includes some centralised code for some common actions that can cause issues if they are implemented in each individual extension.
You can access this extension here: https://github.com/distriqt/ANE-Core.
Android Support
The Android Support libraries encompass the Android Support, Android X and common Google libraries.
These libraries are specific to Android. There are no issues including these on all platforms, they are just required for Android.
This extension requires the following extensions:
androidx.appcompat
androidx.browser
androidx.cardview
androidx.constraintlayout
androidx.core
androidx.exifinterface
androidx.vectordrawable
com.google.android.datatransport
com.google.dagger
com.google.protobuflite
io.reactivex
io.grpc
com.google.code.gson
com.google.guava
com.bumptech.glide
You can access these extensions here: https://github.com/distriqt/ANE-AndroidSupport.
Note: if you have been using the older
com.distriqt.androidsupport.*
(Android Support) extensions you should remove these extensions and replace it with theandroidx
extensions listed above. This is the new version of the android support libraries and moving forward all our extensions will require AndroidX.
Google Play Services
This ANE requires usage of certain aspects of the Google Play Services client library. The client library is available as a series of ANEs that you add into your applications packaging options. Each separate ANE provides a component from the Play Services client library and are used by different ANEs. These client libraries aren't packaged with this ANE as they are used by multiple ANEs and separating them will avoid conflicts, allowing you to use multiple ANEs in the one application.
This ANE requires the following Google Play Services:
You must include the above native extensions in your application along with this extension, and you need to ensure they are packaged with your application.
You can access the Google Play Services client library extensions here: https://github.com/distriqt/ANE-GooglePlayServices.
Firebase
To use Firebase Cloud Messaging you must include the Firebase core and messaging libraries and configure your Firebase application. The core libraries are available in the following extensions alongside the Google Play Services:
See the Firebase configuration documentation on how to configure your application.
If you wish to use Firebase on iOS you must disable the "Hide ANE-Library-Symbols" option in your packaging settings.
We highly discourage usage of this setting as it can increase the size of your application by not correctly identifying code that is shared between extensions.
Any good ANE should not require this setting.
Square ANEs
Due to several of our ANE's using the Square open source libraries the libraries have been separated into a separate ANEs allowing you to avoid conflicts and duplicate definitions. This means that you need to include the some of the square native extensions in your application along with this extension.
You will add these extensions as you do with any other ANE, and you need to ensure it is packaged with your application.
Firebase In-App Messaging requires the following Square extensions:
You can access these extensions here: https://github.com/distriqt/ANE-SquareLibs.
Application Descriptor
- APM
- Manual
Updating your application descriptor will insert the required extensionID
's and generate the manifest and info additions for your application.
You update your application descriptor by running:
apm generate app-descriptor src/MyApp-app.xml
Change the path (src/MyApp-app.xml
) to point to your application descriptor.
If you setup a development build type you can update your app descriptor with this configuration by using :
apm -b development generate app-descriptor src/MyApp-app.xml
Or you could maintain a separate "development" app descriptor by changing the output eg:
apm -b development generate app-descriptor src/MyApp-dev-app.xml
This will modify your application descriptor replacing the manifest additions and info additions with the ones generated from apm
.
You should backup your application descriptor before running this command to ensure you don't lose any information.
If you need to insert custom data into these sections see the guides for Android and iOS
Extension IDs
The following should be added to your extensions
node in your application descriptor to identify all the required ANEs in your application:
The following should be added to your extensions
node in your application descriptor to identify all the required ANEs in your application:
<extensions>
<extensionID>com.distriqt.PushNotifications</extensionID>
<extensionID>com.distriqt.Core</extensionID>
<extensionID>com.distriqt.playservices.Base</extensionID>
<extensionID>com.distriqt.playservices.CloudMessaging</extensionID>
<extensionID>com.google.firebase.core</extensionID>
<extensionID>com.google.firebase.messaging</extensionID>
<extensionID>androidx.browser</extensionID>
<extensionID>androidx.appcompat</extensionID>
<extensionID>androidx.cardview</extensionID>
<extensionID>androidx.constraintlayout</extensionID>
<extensionID>androidx.core</extensionID>
<extensionID>androidx.exifinterface</extensionID>
<extensionID>androidx.vectordrawable</extensionID>
<extensionID>com.google.android.datatransport</extensionID>
<extensionID>com.google.dagger</extensionID>
<extensionID>com.google.protobuflite</extensionID>
<extensionID>com.google.code.gson</extensionID>
<extensionID>com.google.guava</extensionID>
<extensionID>com.bumptech.glide</extensionID>
<extensionID>io.reactivex</extensionID>
<extensionID>io.grpc</extensionID>
<extensionID>com.distriqt.square.okhttp</extensionID>
<extensionID>com.distriqt.square.okhttp3</extensionID>
</extensions>
Android
Manifest Additions
You must add all the Firebase related manifest additions along with several additions for Cloud Messaging.
The following shows the complete manifest additions node. You must replace APPLICATION_PACKAGE
with your
AIR application's Java package name, something like air.com.distriqt.test
.
Generally this is your AIR application id prefixed by air.
unless you have specified no air flair in your build options.
<manifest android:installLocation="auto">
<uses-sdk android:minSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<!-- OPTIONAL -->
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.finsky.permission.BIND_GET_INSTALL_REFERRER_SERVICE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="APPLICATION_PACKAGE.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="APPLICATION_PACKAGE.permission.C2D_MESSAGE" />
<application android:appComponentFactory="androidx.core.app.CoreComponentFactory">
<activity android:name="com.distriqt.core.auth.AuthorisationActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:exported="false" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="false"/>
<!-- NOTIFICATIONS -->
<receiver android:name="com.distriqt.extension.pushnotifications.notifications.receivers.NotificationReceiver" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
<intent-filter>
<action android:name="APPLICATION_PACKAGE.NOTIFICATION_DELETED" />
<action android:name="APPLICATION_PACKAGE.NOTIFICATION_ACTION" />
<data android:scheme="dtpn" />
</intent-filter>
</receiver>
<activity android:name="com.distriqt.extension.pushnotifications.notifications.NotificationActivity" android:exported="false">
<intent-filter>
<action android:name="APPLICATION_PACKAGE.NOTIFICATION_SELECTED" />
<action android:name="APPLICATION_PACKAGE.NOTIFICATION_ACTION" />
<data android:scheme="dtpn" />
</intent-filter>
</activity>
<provider
android:name="com.distriqt.extension.pushnotifications.content.FileProvider"
android:authorities="APPLICATION_PACKAGE.pushnotificationsfileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/distriqt_pushnotifications_paths" />
</provider>
<!-- FIREBASE CLOUD MESSAGING -->
<!--
FirebaseMessagingService performs security checks at runtime,
but set to not exported to explicitly avoid allowing another app to call it.
-->
<service android:name="com.distriqt.extension.pushnotifications.fcm.FcmMessagingService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="com.google.firebase.messaging.FirebaseMessagingService"
android:directBootAware="true"
android:exported="false" >
<intent-filter android:priority="-500" >
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name="com.google.firebase.components.ComponentDiscoveryService"
android:directBootAware="true"
android:exported="false" >
<meta-data
android:name="com.google.firebase.components:com.google.firebase.messaging.FirebaseMessagingRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.datatransport.TransportRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.installations.FirebaseInstallationsRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.abt.component.AbtRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.inappmessaging.display.FirebaseInAppMessagingDisplayRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.inappmessaging.FirebaseInAppMessagingRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
<activity
android:name="com.google.android.gms.common.api.GoogleApiActivity"
android:exported="false"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<provider
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:authorities="APPLICATION_PACKAGE.firebaseinitprovider"
android:exported="false"
android:initOrder="100" />
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true"
android:exported="false" >
</receiver>
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.INSTALL_PACKAGES" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false" />
<service
android:name="com.google.android.gms.measurement.AppMeasurementJobService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />
<!-- datatransport -->
<service
android:name="com.google.android.datatransport.runtime.backends.TransportBackendDiscovery"
android:exported="false" >
<meta-data
android:name="backend:com.google.android.datatransport.cct.CctBackendFactory"
android:value="cct" />
</service>
<service
android:name="com.google.android.datatransport.runtime.scheduling.jobscheduling.JobInfoSchedulerService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" >
</service>
<receiver
android:name="com.google.android.datatransport.runtime.scheduling.jobscheduling.AlarmManagerSchedulerBroadcastReceiver"
android:exported="false" />
</application>
</manifest>
Component Discovery Service
The com.google.firebase.components.ComponentDiscoveryService
is particularly important for Firebase configuration.
The meta-data tags that are added to this service specify the components that should be initialised by Firebase and you must ensure that you have added the appropriate tags for the services you are using.
Firebase Messaging depends on Analytics so you must at least include the following tags:
- Analytics:
<meta-data
android:name="com.google.firebase.components:com.google.firebase.installations.FirebaseInstallationsRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.abt.component.AbtRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
- Messaging (FCM):
<meta-data
android:name="com.google.firebase.components:com.google.firebase.messaging.FirebaseMessagingRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.datatransport.TransportRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
- In-App Messaging:
<meta-data
android:name="com.google.firebase.components:com.google.firebase.inappmessaging.display.FirebaseInAppMessagingDisplayRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.inappmessaging.FirebaseInAppMessagingRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
iOS
Info Additions and Entitlements
Push notifications require a few additions to the Info plist and Entitlements section of your application to correctly configure your application for push notifications.
You should add the listing below to application descriptor iPhone node.
You must replace the BUNDLE_SEED_ID
and BUNDLE_IDENTIFIER
with the information you
gathered when setting up your application. Also make sure you set the environment
correctly either using production or development, both are shown in the example
below with the production version commented out. More on this below.
In order for the Firebase system to work well with AIR and other extensions we need
to disable the automatic delegate proxy that Firebase implements on iOS. To do so
you must set the FirebaseAppDelegateProxyEnabled
option to false
in your InfoAdditions.
<iPhone>
<InfoAdditions><![CDATA[
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
]]></InfoAdditions>
<requestedDisplayResolution>high</requestedDisplayResolution>
<Entitlements><![CDATA[
<!-- DEVELOPMENT -->
<key>get-task-allow</key>
<true/>
<key>aps-environment</key>
<string>development</string>
<!-- PRODUCTION -->
<!--
<key>get-task-allow</key>
<false/>
<key>aps-environment</key>
<string>production</string>
-->
<key>application-identifier</key>
<string>BUNDLE_SEED_ID.BUNDLE_IDENTIFIER</string>
<key>keychain-access-groups</key>
<array>
<string>BUNDLE_SEED_ID.*</string>
</array>
]]></Entitlements>
</iPhone>
The first entitlement field is the aps-environment
. This field indicates whether
we are using the development or the production environment. It must be either
development
or production
and depends on which configuration you are using.
If you are running a debug build you should use development. If you are looking
to publish the application to the AppStore, you should use production.
You should have noted the BUNDLE_SEED_ID
(or App ID Prefix) and BUNDLE_IDENTIFIER
when you were setting up your application in the iOS developer center.
The seed id should be a unique ten character string and the identifier should be
similar to your AIR application id.
Note the difference between a development and production / adhoc entitlements. The get-task-allow
and aps-environment
must be set correctly for your build.
These values must match the values in your provisioning profile, ie you must have setup push notifications for the matching environment.