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:

To send push notifications through your Dashboard, you will have to create a Project at Firebase Website and link it to your Android Studio Project. To do so, follow the steps described below:

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

  1. Go to Firebase Website and log in with a Google Account.
  2. At Firebase Website, in the right corner click on GO TO CONSOLE and click on Add Project, then give your Project a name and click on the CREATE PROJECT button.
  3. Then, connect your Android Studio Project to the Firebase Project you created. To do so, click on the Android icon, as shown in the following image.
  4. You will be asked to inform the package name of your Android Studio Project, as shown in the following image.
  5. To discover the package name of your Android Studio Project, leave the Firebase page opened and go to your Project in Android Studio and go to app > manifest > AndroidManifest.xml. In your manifest file you will be able to find the package name of your project, as you can see in the image below.
  6. Copy the package name in the required box at the Firebase page. You can also fill the other fields, but they are optional. After that, click on the Register app button.
  7. Now, you have to download google-services.json file and move it to your Android Studio project module root directory.
  8. To use the Google services plugin for Gradle and load the google-services.json file you just downloaded, modify your build.gradle files. First, go to the build.gradle (Project) file and in the dependencies tag add the following code.
    1
       classpath 'com.google.gms:google-services:4.0.1'
    
  9. After that, go to the build.gradle (Module:app) file and, on the top of the file, add the code below.
    1
       apply plugin: 'com.google.gms.google-services'
    
  10. Continue on the build.gradle (Module:app)` file and add these lines of code:
    1
    2
    3
       // Don't forget to change the line below with the latest versions of Firebase SDKs
       implementation 'com.google.firebase:firebase-core:latest.version.here'
       implementation 'com.google.firebase:firebase-messaging:latest.version.here'
    

    Don’t forget to change these lines with the latest versions of Firebase SDKs.

To link your Firebase Project with Back4App and easily send push notification through your Dashboard, simply follow these steps:

  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:
  3. Leave the Back4App Android Push Notification page you visited opened and go to your project at Firebase Website.
  4. Click on the settings icon and then click on the Project Settings button, as shown below.
  5. 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.
  6. To finish just click on EDIT at Back4App Website.

Step 3 - Set up the Manifest File

  1. Open your Project at Android Studio and go to app > manifest > AndroidManifest.xml. In this file, use the code below right after the meta-data tags that are inside the application tag:
    1
    2
    <meta-data android:name="com.parse.push.gcm_sender_id"
               android:value="id:INSERT_YOUR_SENDER_ID" />
    

    Don’t forget to insert the GMC Sender ID you obtained at Firebase in this line of code.

  2. 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
    17
    18
    19
    20
    21
    22
    <service
       android:name="com.parse.fcm.ParseFirebaseInstanceIdService"
       android:exported="true">
       <intent-filter>
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
       </intent-filter>
    </service>
    
    <service
        android:name="com.parse.fcm.ParseFirebaseMessagingService">
        <intent-filter>
             <action android:name="com.google.firebase.MESSAGING_EVENT"/>
         </intent-filter>
     </service>
    
     <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>    
    
  3. 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 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 4 - Set up build.gradle (Module: app)

Install the Parse FCM SDK and the Parse Bolts SDK for Android. To do so, open build.gradle (Module: app) and add the code below in the dependecies{} tag.

1
2
3
  // Don't forget to change the lines belows with the latest versions these SDKs
  implementation "com.github.parse-community.Parse-SDK-Android:fcm:latest.version.here"
  implementation 'com.parse.bolts:bolts-android:latest.version.here'

Don’t forget to change these lines with the latest versions of these SDKs.

Step 5 - Create an installation

Every Parse application installed on a device registered for push notifications has an associated Installation object that stores all the data needed to target push notifications.

In Android, Installation objects are available through the ParseInstallation class. This class uses the same API for storing and retrieving data. To access the current Installation object from your Android app, use the ParseInstallation.getCurrentInstallation() method.

In the first time you save a ParseInstallation, Parse will add it to your Installation class and it will be available for targeting push notifications.

To create a ParseInstallation in your app, go to your Android Studio Project and in the Java file called App that extends Application that you created to initialize the Parse SDK, on its onCreate method, right after Parse.initialize() call, use the following code to create a ParseInstallation.

1
2
3
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("GCMSenderId", INSERT_YOUR_SENDER_ID);
    installation.saveInBackground();

Don’t forget to insert the GMC Sender ID you obtained at Firebase in the code above.

If you don’t have an App.java file as described in this step, access the Install Parse SDK for Android documentation and be sure that you have followed all the steps required to install Parse SDK correctly, otherwise, your twitter login may not work properly.

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

    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!