Android
Push Notifications

using Cloud Code

16min

Send Parse push notifications using Cloud Code

Introduction

This section explains how you can send push notifications using Cloud Code through Back4App.

This is how it will look like:

Document image


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

Prerequisites

To complete this tutorial, we need:

1 - Set up Android to receive push

Every Parse application installed on a device registered for push notifications has an associated Installation object. The Installation object is where you store all the data needed to target push notifications. For example, in your app, you could store which teams one of your users is interested in to send updates about their performance. Saving the Installation object is also required for tracking push-related app open events.

The simplest way to start sending notifications is using channels. This allows you to use a publisher-subscriber model for sending pushes. Devices start by subscribing to one or more channels, and notifications can later be sent to these subscribers. The channels subscribed to by a given Installation are stored in the channels field of the Installation object.

To start working with push notifications, the following steps are required:

If you downloaded our project template, don’t forget to change your credentials in the app/src/main/res/values/string.xml file and the GCMSenderId that you obtained at Firebase in the AndroidManifest.xml file.

  1. Import the following dependecies:
1 // Java Dependencies 2 import java.util.ArrayList; 3 // Parse Dependencies 4 import com.parse.Parse; 5 import com.parse.ParseInstallation;

2. Initialize Parse with Parse.initialize(this).

3. Create a new array of channels and put the channels you would like to subscribe. In this example, the News channel is created.

4. Add to your installation your GCMSenderId, obtained from the Firebase Console, through the command installation.put("GCMSenderId", "YOUR_FIREBASE_GCM_SENDER_ID_HERE").

To know how you can obtain that key, look at Step 1 of Push Notifications via Dashboard tutorial.

5. Add the channels object to the installation through the command installation.put("channels", channels).

6. Save the installation to your database through installation.saveInBackground().

The following code executes these steps:

Java


2 - Create your Cloud Code

To know more about how to get started with Cloud Code look at Cloud Code for Android Tutorial.

  1. Create a .jsfile to put your Cloud Code into. In this example, a main.js file is created.
  2. Define a Cloud function, using Parse.Cloud.Define, to call the push notification. In this example, this function is called Parse.Push.Send.

It is required to use the master key in this operation.

The following code executes these steps:

Parse Server 3.X
Parse Server 2.X


3 - Upload to Cloud Code

  1. Go to your App at Back4App website and click on Dashboard.
  2. Find the Cloud Code and click on Functions & Web Hosting. It looks like this:
Document image


3. Upload or create a new file (you can also edit the current main.js file directly on the browser). Then, click at Deploy as shown here:

Document image


4 - Call Cloud Code from Android App

  1. Import the following dependencies:
1 // Java Dependencies 2 import java.util.HashMap; // This includes the HasMap Object that the 4 Cloud function needs to call 3 // Parse Dependencies 4 import com.parse.FunctionCallback; 5 import com.parse.ParseCloud; 6 import com.parse.ParseException;

2. Call the ParseCloud.callFunctionInBackground on the pushsample cloud function:

Java


The alertDisplayer method used in the example above is the following:

1 private void alertDisplayer(String title,String message){ 2 AlertDialog.Builder builder = newAlertDialog.Builder(MainActivity.this) 3 .setTitle(title) 4 .setMessage(message) 5 .setPositiveButton("OK", new DialogInterface.OnClickListener() { 6 @Override 7 public void onClick(DialogInterface dialog, int which) { 8 dialog.cancel(); 9 } 10 }); 11 AlertDialog ok = builder.create(); 12 ok.show(); 13 }

3. Test if the push notifications are being sent by calling the function above while the device is opened.

5 - Call Cloud Code from REST API

The REST API provides a quick and easy way to test if your Cloud function is working. Just use the code below in your terminal or command prompt:

Click on to know more about how to get started with command line in Linux, MacOS or Windows.

curl -X POST -H "X-Parse-Application-Id: YOUR_APP_ID_HERE" \ -H "X-Parse-REST-API-Key: YOUR_REST_API_KEY_HERE" \ -H "Content-Type: application/json" \ -d '{ // Put the function parameters here in JSON format }' \ https://parseapi.back4app.com/functions/pushsample

To test the push notifications, just use the REST code while the device is opened.

It’s done!

At this stage, you can send push notifications using Cloud Code through Back4App!