Set up a Firebase Cloud Messaging client app with C++

To write your cross-platform Firebase Cloud Messaging client app with C++, use the
Firebase Cloud Messaging API.
The C++ SDK works for both Android and iOS, with some additional setup required
for each platform.

Define a class that implements the firebase::messaging::Listener
interface.

Initialize FCM, passing in the App and a constructed Listener:

::firebase::messaging::Initialize(app, listener);

Apps that rely on the Google Play services SDK should check the device
for a compatible Google Play services APK before accessing the features.
To learn more, refer to
Check for Google Play services APK.

Drag the firebase.framework and firebase_messaging.framework
frameworks into your Xcode project from the Firebase C++ SDK.

Configure your Xcode project to enable Push Notifications:

Select the project from the Navigator area.

Select the project target from the Editor area.

Select the General tab from the Editor area.

Scroll down to Linked Frameworks and Libraries, then click
the + button to add frameworks.

In the window that appears, scroll to
UserNotifications.framework, click on that entry, then click
Add.

This framework only appears in Xcode v8 and later and is
required by this library.

Select the Capabilities tab from the Editor area.

Switch Push Notifications to On.

Scroll down to Background Modes, then switch it to On.

Select Remote notifications under Background Modes.

Create a Firebase App object:

app = ::firebase::App::Create(::firebase::AppOptions());

Define a class that implements the firebase::messaging::Listener
interface.

Initialize Firebase Cloud Messaging, passing in the App and a constructed
Listener:

::firebase::messaging::Initialize(app, listener);

Access the device registration token

Upon initializing the Firebase Cloud Messaging library, a registration token is
requested for the client app instance. The app will receive the token with the
OnTokenReceived callback, which should be defined in the class that implements
firebase::messaging::Listener.

If you want to target that specific device, you'll need access to this token.

Note about message delivery on Android

When the app is not running at all and a user taps on a notification,
the message is not, by default, routed through FCM's built in
callbacks. In this case, message payloads are received through an Intent
used to start the application. To have FCM forward these incoming
messages to the C++ library callback, you need to override the method
onNewIntent in your Activity and pass the Intent to the
MessageForwardingService.

Messages received while the app is in the background have the content of
their notification field used to populate the system tray notification, but
that notification content will not be communicated to FCM. That is,
Message::notification will be a null.

In summary:

App state

Notification

Data

Both

Foreground

OnMessageReceived

OnMessageReceived

OnMessageReceived

Background

System tray

OnMessageReceived

Notification: system tray
Data: in extras of the intent.

Custom Message Handling on Android

By default, notifications sent to the app are passed to
::firebase::messaging::Listener::OnMessageReceived, but in some cases you may
want to override the default behavior. To do this on Android you will need to
write custom classes that extend
com.google.firebase.messaging.cpp.ListenerService as well as update your
project's AndroidManifest.xml.

Override ListenerService Methods.

The ListenerService is the Java class that intercepts incoming messages sent to
the app and routes them to the C++ library. When the app is in the foreground
(or when the app is the background and it receives a data-only payload),
messages will pass through one of the callbacks provided on this class. To add
custom behavior to the message handling, you will need to extend FCM's
default ListenerService:

Update AndroidManifest.xml

Once your custom classes have been written, they must be included in the
AndroidManifest.xml to take effect. Ensure that the manifest includes the
merge tools by declaring the appropriate attribute inside the <manifest> tag,
like so:

In the firebase_messaging_cpp.aar archive there is an AndroidManifest.xml
file which declares FCM's default ListenerService. This manifest
is normally merged with the project specific manifest which is how the
ListenerService is able to run. This ListenerService needs to replaced with
the cusom listener service. That is accomplished by removing the default
ListenerService and adding the custom Service, which can be done with the
following lines your projects AndroidManifest.xml file:

Prevent auto initialization

FCM generates an Instance ID, which is used as a registration
token within FCM. When an Instance ID is generated the library will upload the
identifier and configuration data to Firebase.If you want to get an explicit
opt-in before using Instance ID, you can prevent generation at configure time by
disabling FCM (and on Android, Analytics). To do this, add a metadata value to
your Info.plist (not your GoogleService-Info.plist) on iOS, or your
AndroidManifest.xml on Android:

iOS

Handling Messages with Deep Links on Android

FCM allows messages to be sent containing a deep link into your app.
To receive messages that contain a deep link, you must add a new intent filter
to the activity that handles deep links for your app. The intent filter should
catch deep links of your domain. If your messages do not contain a deep link,
this configuration is not necessary. In AndroidManifest.xml:

When users tap a notification containing a link to the scheme and host you
specify, your app will start the activity with this intent filter to handle the
link.

Next steps

After setting up the client app, you are ready to send downstream and topic
messages with the Firebase. To learn more, see this functionality demonstrated
in the
quickstart sample
which you can download, run, and review.

To add other, more advanced behavior to your app see the guides for sending
messages from an app server: