Navigation
  • 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.

Procedure

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

1

Create a Firebase Project.

Go to https://console.firebase.google.com/ and add a project. Upon successful creation, you will be redirected to your Firebase project overview page.

2

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.

3

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 MainActivity.java located in app/src/main/java/com/mongodb/example/pushnotification.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        stitchClient = new StitchClient(this, "STITCH-APP-ID");
        progressBar = (ProgressBar)findViewById(R.id.progress_bar);
        progressBar.setIndeterminate(true);
    
  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 -->
<service
    android:name=".MyGCMListenerService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>

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 MainActivity.java the following subscribes to the topic described by the checkbox label:

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

             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