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.

Set up Firebase and the FCM SDK

Android

  1. FCM client apps require devices running Android 4.0 or higher that also have the Google Play Services application installed, and that are built with Gradle using Android Studio 1.4 or higher.
  2. If you haven't already, add Firebase to your C++ project.
  3. In Android Studio, add the FCM dependency to your app-level build.gradle file:
    repositories {
      flatDir {
        dirs project.ext.firebase_cpp_sdk_dir + "/libs/android"
      }
    }
    dependencies {
         compile 'com.google.firebase:firebase-messaging:11.2.0'
         compile 'com.google.firebase.messaging.cpp:firebase_messaging_cpp@aar'
    }
  4. Link in the libapp.a and libmessaging.a static library, from the C++ SDK.
  5. Create a Firebase App object, passing in the JNI environment and Activity:
    app = ::firebase::App::Create(::firebase::AppOptions(), jni_env, activity);
    
  6. Define a class that implements the firebase::messaging::Listener interface.
  7. Initialize the Firebase Cloud Messaging, passing in the App and a constructed Listener:
    ::firebase::messaging::Initialize(app, listener);
    
  8. Apps that rely on the Play Services SDK should check the device for a compatible Google Play services APK before accessing the features. To learn more, see Check for Google Play Services APK.

iOS

  1. You need a valid APNs certificate. If you don't already have one, see Provision APNs SSL Certificates.
  2. If you haven't already, add Firebase to your C++ project.
  3. In your project's Podfile, add the FCM dependency:
    pod 'Firebase/Messaging'
  4. Drag the firebase.framework and firebase_messaging.framework frameworks into your Xcode project, from the C++ SDK.
  5. Create a Firebase App object:
    app = ::firebase::App::Create(::firebase::AppOptions());
  6. Define a class that implements the firebase::messaging::Listener interface.
  7. 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.

import com.google.firebase.messaging.MessageForwardingService;

class MyActivity extends Activity {
  private static final String TAG = "MyActvity";

  @Override
  protected void onNewIntent(Intent intent) {
    Log.d(TAG, "A message was sent to this app while it was in the background.");
    Intent message = new Intent(this, MessageForwardingService.class);
    message.setAction(MessageForwardingService.ACTION_REMOTE_INTENT);
    message.putExtras(intent);
    startService(message);
  }
}

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:

import com.google.firebase.messaging.cpp.ListenerService;

class MyListenerService extends ListenerService {

By overriding the method ListenerService.onMessageReceived, you can perform actions based on the received RemoteMessage object and get the message data:

@Override
public void onMessageReceived(RemoteMessage message) {
  Log.d(TAG, "A message has been received.");
  // Do additional logic...
  super.onMessageReceived(message);
}

ListenerService also has a few other methods that are used less frequently. These can be overridden as well, for more information see the FirebaseMessagingService reference.

@Override
public void onDeletedMessages() {
  Log.d(TAG, "Messages have been deleted on the server.");
  // Do additional logic...
  super.onDeletedMessages();
}

@Override
public void onMessageSent(String messageId) {
  Log.d(TAG, "An outgoing message has been sent.");
  // Do additional logic...
  super.onMessageSent(messageId);
}

@Override
public void onSendError(String messageId, Exception exception) {
  Log.d(TAG, "An outgoing message encountered an error.");
  // Do additional logic...
  super.onSendError(messageId, exception);
}

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:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.firebase.messaging.cpp.samples"
    xmlns:tools="http://schemas.android.com/tools">

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:

<service android:name="com.google.firebase.messaging.cpp.ListenerService"
         tools:node="remove" />
<service android:name="com.google.firebase.messaging.cpp.samples.MyListenerService"
         android:exported="false">
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
  </intent-filter>
</service>

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:

Keep in mind that you'll need a server implementation to make use of these features.

Send feedback about...

Need help? Visit our support page.