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:
At any time, you can access the complete Project via our GitHub repositories.
Prerequisites
To complete this tutorial, you need:
- Android Studio.
- An app created on Back4App.
- Note: Follow the New Parse App Tutorial to learn how to create a Parse App on Back4App.
- An android app connected to Back4app.
- Note: Follow the Install Parse SDK Tutorial to create an Android Studio Project connected to Back4App.
- A device (or virtual device) running Android 4.0 (Ice Cream Sandwich) or newer.
Step 1 - Link your Firebase Project with your Android Studio Project
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.
- Go to Firebase Website and log in with a Google Account.
- At Firebase Website, in the right corner click on
GO TO CONSOLE
and click onAdd Project
, then give your Project a name and click on theCREATE PROJECT
button. - 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.
- You will be asked to inform the package name of your Android Studio Project, as shown in the following image.
- 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. - 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. - Now, you have to download
google-services.json
file and move it to your Android Studio project module root directory. - To use the Google services plugin for Gradle and load the
google-services.json
file you just downloaded, modify yourbuild.gradle
files. First, go to thebuild.gradle (Project)
file and in the dependencies tag add the following code.1
classpath 'com.google.gms:google-services:latest.version.here'
- 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'
- 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.
Step 2 - Link your Firebase Project with Back4App
To link your Firebase Project with Back4App and easily send push notification through your Dashboard, simply follow these steps:
- Go to Back4App Website, log in, find your app and click on
Server Settings
. - Find the “Android Push notification” block and click on
SETTINGS
>EDIT
. The “Android Push notification” block looks like this: - Leave the Back4App Android Push Notification page you visited opened and go to your project at Firebase Website.
- Click on the settings icon and then click on the
Project Settings
button, as shown below. - Click on
CLOUD MESSAGING
, copy theServer Key
and paste it at theAPI Key
field shown in the Back4App Android Push Notification page you visited. Do the same thing with theSender ID
, copy and paste it at theGCM Sender ID
field. - To finish just click on
EDIT
at Back4App Website.
Step 3 - Set up the Manifest File
- Open your Project at Android Studio and go to
app
>manifest
>AndroidManifest.xml
. In this file, use the code below right after themeta-data
tags that are inside theapplication
tag:1 2
<meta-data android:name="com.parse.push.gcm_sender_id" android:value="INSERT_YOUR_SENDER_ID" />
Don’t forget to insert the
GCM Sender ID
you obtained at Firebase in this line of code. - Use the following code right before the
application
tag ends:
AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<service android:name="com.parse.fcm.ParseFirebaseMessagingService" android:exported="false">
<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>
AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<service android:name="com.parse.fcm.ParseFirebaseInstanceIdService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name="com.parse.fcm.ParseFirebaseMessagingService" android:exported="false">
<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>
- 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.
If you are not using AndroidX, you cannot use the latest version. Check its changelog
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
GCM 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
-
Go to Back4App Website, log in, find your app and click on
Dashboard
. - Click on
More
>Push
>Send New Push
and create an audience for your push notification. - Write your message and look at the preview by clicking at
Android
option. - 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 atPast Pushes
you sent and theAudiences
you created for them.
It’s done!
At this stage, you can send push notifications using Parse Dashboard
through Back4App!