• Tutorials >
  • Push Notifications (Android App)

Push Notifications (Android App)

This tutorial demonstrates how to use MongoDB Stitch to receive push notifications in an Android app.


A. Create a MongoDB Stitch Project

Log in to Atlas and create a new MongoDB Stitch app. If you have not set up an Atlas cluster or a MongoDB Stitch application, see steps in Getting Started.

B. Set up Firebase Project


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 Project 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 Push Notifications in the MongoDB Stitch console.

C. Configure Push Notification for Your MongoDB Stitch Application

Push Notifications in MongoDB Stitch require a Google Cloud Messaging (GCM) service. Create and configure a GCM service in Stitch with the following procedure:

  1. Navigate to the MongoDB Stitch console.
  2. Click on Push Notifications in the left navigation pane.
    • Click the Config tab.
    • In Sender ID, enter your Firebase project’s Sender ID.
    • In API Key, enter your Firebase project’s Legacy server key.
  3. Click Save.

D. Set up the Example Android App

  1. Download the code for the example app from the GitHub example repostory.

  2. Open the app contained in the directory StitchSamplePushNotificationApp in Android Studio.

  3. Edit the file located in app/src/main/java/com/mongodb/example/pushnotification.

    protected void onCreate(Bundle savedInstanceState) {
        stitchClient = new StitchClient(this, "STITCH-APP-ID");
        progressBar = (ProgressBar)findViewById(;
  4. Replace STITCH-APP-ID with the App ID for your app. Your App ID can be found on the Clients page of the Stitch console.

  5. Run the App in an Android emulator or phone supporting SDK version 19 or higher. It should look like the following.

    The Example App Running
  6. Toggle any of the three topics to subscribe for notifications.

E. Review Code

The GCM listener service is specified in the app AndroidManifest.xml.

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

The method onPushMessageReceived in MyGCMListenerService is called when messages from subscribed topics are received. When the app is in the background, GCM creates system tray notifications by default. When the app is in the foreground, code in onPushMessageReceieved creates a system tray notification.

In the following subscribes to the topic described by the checkbox label:

if (checkBox.isChecked()) {
     pushClient.subscribeToTopic(topic).addOnCompleteListener(new OnCompleteListener<Void>() {
         public void onComplete(@NonNull final Task<Void> task) {
             if (!task.isSuccessful()) {
                 Log.d(TAG, "Error subscribing to topic " + task.getException());

             Log.d(TAG, "Subscribed to topic " + topic);
             Toast.makeText(getApplicationContext(), "Subscribed to topic " + topic, Toast.LENGTH_LONG).show();

For more information see Push Notifications (GCM).

F. Create Push Notifications in MongoDB Stitch

  1. In the Stitch console, navigate to Push Notifications.

  2. Click Send New Notification.

  3. 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 a topic that has been subscribed from the app. Prefix with “/topics/”. E.g. “/topics/quotes”.
  4. Click Send Message.

  5. If the topic has been subscribed to in the app, a notification will appear with the message text.

    The Example App Receiving Notification