Push Notifications

MongoDB Stitch supports integrating Android apps with Google Cloud Messaging (GCM) service to provide push notifications.

Set up Firebase Project

To integrate with your MongoDB Stitch app, you need a Firebase project (preferred for new projects) or Google Cloud Messaging project (if your app is already using GCM).


Create a Firebase Project.

Go to and add a project. Upon successful creation, you will be redirected to your Firebase project overview page.


Add Firebase to your Android app.

  1. Click Add Firebase to your Android app.

  2. Enter the requested information to register your app and click Skip to the Console (i.e., skip both the download config file step and add Firebase SDK step).

    The console displays your Firebase project overview page.


Get Cloud Messaging credentials.

  1. On the left-hand navigation bar, click on the the gear icon next to Overview and select Project settings.
  2. Click Cloud Messaging.
  3. Copy the Legacy server key and the Sender ID. You will use the information when configuring the GCM service in the MongoDB Stitch console.

Go to the MongoDB Stitch console.

Configure Push Notification for Your MongoDB Stitch Application


Configure the Push Notifications service.

  1. Click on the Push Notifications service.
  2. Click the Config tab and enter the following:
    • In Sender ID, enter your Firebase project’s Sender ID.
    • In API Key, enter your Firebase project’s Legacy server key.
  3. Click Save.

Create a Push Notification

To create a notification:

  1. Click Send New Notification.

  2. Enter the following information:

    Message Text Enter the message to send. Limit to 250 characters.
    Message Label Enter a descriptive identifier for the message.
    Topic Specify the topic to send messages to users who have opted into the specified topic.
  3. Click either:

    • Save Draft to continue working on later

    - or -

    • Send Message to send the message immediately.

View Sent Messages

From the Sent tab, you can view a list of the sent messages. For the sent messages:

  • Resend sent messages.
  • Duplicate sent messages.

View Draft Messages

In the Draft tab, you can view a list of the draft messages. For the draft messages, you can:

  • Duplicate draft messages.
  • Edit draft messages.
  • Delete draft messages.

Create Rules

You can add rules to control conditions regarding when to send. See GCM Service Rules for more information.

Integrating into Application Code

To receive push notification, in your Android application code:


The following is a numbered list of code snippets and project configuration for incorporating push notification into your Android application. The list is not meant to be a step-by-step guide.

  1. In your application’s build.gradle file:

    1. Add the following maven repository:

      repositories {
          maven {
              url ""
          //...  Additional repositories as required for your project
    2. Add the following dependency for MongoDB Stitch:

      dependencies {
          compile 'org.mongodb:stitch:0.1.0-SNAPSHOT'
          //...  Additional dependencies as required for your project
    3. Add the following dependency for GCM:

      dependencies {
          compile 'org.mongodb:stitch:0.1.0-SNAPSHOT'
          //...  Additional dependencies as required for your project
          compile ''
  2. In your client code, declare and initialize a connection to Stitch:

    StitchClient stitchClient = new StitchClient(this, "<your-app-id>");

    where <your-app-id> is your MongoDB Stitch app ID. In the MongoDB Stitch console, you can find your App ID in the Clients view.

  3. In your client code, add logic to authenticate to MongoDB Stitch. Use logInWithProvider() method to authenticate.

    For example, if your MongoDB Stitch application allows Anonymous authentication, include the following code to log in anonymously:

    stitchClient.logInWithProvider(new AnonymousAuthProvider());
  4. In your client code, add code to create a GCMPushClient using the Firebase project’s Sender ID and Legacy server key stored in MongoDB Stitch for your app’s GCM service.

    GCMPushClient pushClient = (GCMPushClient) stitchClient.getPush()

    Alternatively, StitchClient can read the Sender ID and Legacy server key from a properties file To use the properties file instead:

    • Set push.gcm.senderId and push.gcm.service properties in the file.

    • Use the following code to create a GCM Push Provider:

      GCMPushClient pushClient = (GCMPushClient) stitchClient.getPush()
  5. Use the GCMPushClient’s register() method to register the client for push notifications. For example:

    pushClient.register().addOnCompleteListener(new OnCompleteListener<Void>() {
        public void onComplete(@NonNull final Task<Void> task) {
            if (!task.isSuccessful()) {
               // ...

    See also the deregister() method to deregister the client from push notification.

  6. To subscribe to topics, use GCMPushClient’s subscribeToTopic() method. For example:


    See also the subscribeToTopic() method.

  7. Create a class that extends the abstract GCMListenerService and implement the onPushMessageReceived() method.

    public class MyGCMListenerService extends GCMListenerService {
        public void onPushMessageReceived(PushMessage message) {
           // Implementation if overriding
  8. Update AndroidManifest.xml file for your Android project to register your GCMListenerService and a receiver for your application. Add the following inside the <application> element:

            <action android:name="" />
            <category android:name="" />
    <!-- Name of your GCM ListenerService -->
            <action android:name="" />

    Substitute your package name for "" and your GCMListenerService for the service name.

  9. Optional. You can also add a WAKE_LOCK user-permission in the AndroidManifest.xml file to give the service a chance to process the message before sleeping.

    <uses-permission android:name="android.permission.WAKE_LOCK" />