Android

Parse Server push notifications setup

Introduction

This section explains how you can send push notifications using Firebase Cloud Messaging and Parse Dashboard through Back4App.

This is how it will look like:

Push Notification Via Dasboard App

At any time, you can access the complete Android Project built with this tutorial at our GitHub repository.

Prerequisites

To complete this tutorial, you need:

  • Android Studio.
  • An app created on Back4App.
  • An android app connected to Back4app.
  • A Project created at Firebase.
  • A device (or virtual device) running Android 4.0 (Ice Cream Sandwich) or newer.

Pay attention to the steps below because you are not going to follow exactly the same steps that Firebase suggests.

  1. Go to Back4App Website, log in, find your app and click on Server Settings.
  2. Find the “Android Push notification” block and click on SETTINGS > EDIT. The “Android Push notification” block looks like this:

    Android Push Notification Settings at Back4App

  3. Leave the Back4App Android Push Notification page you visited opened and go to Firebase Website and log in with a Google Account.
  4. At Firebase Website, in the right corner click on GO TO CONSOLE and click on Add Project, then give your Project a name.

    Login Firebase

  5. Click on the settings icon next to Project Overview and then click on Project Settings, as shown below:

    Firebase Settings

  6. Click on GENERAL > Add Firebase to your Android app icon, fill in the fields with your Android package name (you can find it in /app/manifest/AndroidManifest.xml file in your Android Studio Project) and click on REGISTER APP.

    Add App to Firebase

  7. Click on CLOUD MESSAGING, copy the Server Key and paste it at the API Key field shown in the Back4App Android Push Notification page you visited. Do the same thing with the Sender ID, copy and paste it at the GCM Sender ID field.

    Firebase Cloud Keys

  8. To finish just click on NEXT at Back4App Website.

Step 2 - Set up the Manifest File

  1. Open your Project at Android Studio and go to /app/manifest/AndroidManifest.xml.
  2. Right after the application tag use:
    1
    2
     <!--don't forget to change the line below with the name of the application you will use to set up the Push Notifications-->
     android:name=".INSERT_NAME_OF_YOUR_JAVA_CLASS"
    
  3. Use the code below right after the meta-data tags that are inside the application tag:
    1
    2
    3
    <meta-data android:name="com.parse.push.gcm_sender_id"
               android:value="id:INSERT_YOUR_SENDER_ID" />
     <!--don't forget to change the line above with the Sender ID you obtained at Firebase-->
    
  4. Use the following code right before the application tag ends:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <service android:name="com.parse.PushService" />
         <receiver android:name="com.parse.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
             <intent-filter>
                 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                 <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                 <!--don't forget to change the line below with the package name of your app-->
                 <category android:name="INSERT_YOUR_PACKAGE_NAME" />
             </intent-filter>
         </receiver>
         <receiver android:name="com.parse.ParsePushBroadcastReceiver" android:exported="false">
             <intent-filter>
                 <action android:name="com.parse.push.intent.RECEIVE" />
                 <action android:name="com.parse.push.intent.OPEN" />
                 <action android:name="com.parse.push.intent.DELETE" />
             </intent-filter>
         </receiver>      
    
  5. Use the following permissions right after the uses-permission tags that you placed to allow your app to have access to internet:
    1
    2
    3
    4
    5
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    

    You added these permissions to allow internet access in the Install Parse SDK Tutorial instructions. If you didn’t, access Install Parse SDK Tutorial and follow its steps.

Step 3 - Set up build.gradle (Module: app)

  1. Open build.gradle (Module: app).
  2. Add the code below right before the dependecies{} section ends:
    1
    2
    3
    compile 'com.parse.bolts:bolts-android:1.4.0'
    compile 'com.google.firebase:firebase-core:11.8.0'
    compile 'com.google.firebase:firebase-messaging:11.8.0'
    
  3. Add the following code right before the repositories{} section ends:
    1
    2
    3
    4
    jcenter()
    maven {
         url "https://maven.google.com"
    }
    

    You added the repositories{} section in this build.gradle because of the Install Parse SDK Tutorial instructions.
    If you didn’t, access Install Parse SDK Tutorial and follow its steps.

Step 4 - Set up build.gradle (Project)

  1. Open build.gradle (Project).
  2. Inside the dependencies{} section, add the following code:
    1
    classpath 'com.google.gms:google-services:3.1.0'
    

Step 5 - Implement a Push Application

  1. Create a new Application to set up push notifications and name it with the same name you set for it in the Manifest file.
  2. Import at the beginning of the class the following:
    1
    2
    3
    4
    5
    // Front End Dependencies
    import android.app.Application;
    // Parse Dependencies
    import com.parse.Parse;
    import com.parse.ParseInstallation;
    
  3. To implement push notifications, simply use the code below:
    1
    2
    3
    4
    5
    Parse.initialize(this);
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    // don't forget to change the line below with the sender ID you obtained at Firebase.
    installation.put("GCMSenderId", "INSERT_YOUR_SENDER_ID");
    installation.saveInBackground();
    

Step 6 - Test your app

  1. Go to Back4App Website, log in, find your app and click on Dashboard.
  2. Click on Push > Send New Push and create an audience for your push notification.

    Push at Dashboard

  3. Write your message and look at the preview by clicking at Android option.
  4. If you have already reviewed the push notification and you want to send, click on Send push.

    Send Push

You may explore the other options for Push Notification at Parse Dashboard.
There, it’s also possible to look at Past Pushes you sent and the Audiences you created for them.

It’s done!

At this stage, you can send push notifications using Parse Dashboard through Back4App!