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:
    dependencies {
         compile 'com.google.firebase:firebase-messaging:10.2.0'
    }
  4. Link in the libapp.a and libmessaging.a static library, from the C++ SDK. Add the following to your application's AndroidManifest.xml:
    <service android:name="com.google.firebase.messaging.cpp.ListenerService">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
    </service>
    <!-- This service must be in the same process as the ListenerService -->
    <service android:name="com.google.firebase.messaging.cpp.RegistrationIntentService"
             android:exported="false" >
    </service>
    <service android:name="com.google.firebase.messaging.cpp.FcmInstanceIDListenerService">
      <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
      </intent-filter>
    </service>
    
    The ListenerService is used to route incoming messages to the SDK, which then passes them to your app. The FcmInstanceIDListenerService and RegistrationIntentService are used for receiving and requesting a registration token, respectively. The ListenerService and the RegistrationIntentService are required to be in the same process as the activity that runs FCM native library. They run in the same process by default, but if you specify the process for the activity or one of the services you must do the same for the others as well.
  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.

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.