Android

Send Parse push notifications from client side

Introduction

Client Push is a feature that is available on Back4App’s Parse API, however it is not enabled by default due to security issues.

Enabling Client Push and allowing your App to use its features is quite simple, but not encouraged. The main function of Client Push is for debugging and test purposes.

In this tutorial an example app will be built and this is how it will look like:

Client Push 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:

Step 1 - Enable Client Push

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

    Back4app features page

  3. Scroll to the end of the page and click on the EDIT DETAILS button, as shown below:

    Your parse app details

  4. You will see a checkbox called Allow Push Notification from Client in the end of the edit page, tick that box and click on the SAVE button, as shown below:

    Your parse app details

Step 2 - Push from your app

  1. At the beginning of your activity, import the following dependencies:
    1
    2
    3
    4
    5
    // Imports to the JSONObject object, necessary for the push message
    import org.json.JSONException;
    import org.json.JSONObject;
    // Parse Dependencies
    import com.parse.ParsePush;
    
  2. Use ParsePush to send the push message, as shown in the following code:

    Remember to set up the channels and the message.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    JSONObject data = new JSONObject();
    // Put data in the JSON object
    try {
     data.put("alert", "Back4App Rocks!");
     data.put("title", "Hello from Device");
    } catch ( JSONException e) {
     // should not happen
     throw new IllegalArgumentException("unexpected parsing error", e);
    }
    // Configure the push
    ParsePush push = new ParsePush();
    push.setChannel("News");
    push.setData(data);
    push.sendInBackground();
    
  3. To test the push notifications, just call that function while the device is opened.

It’s done!

At this stage, you can send push notifications using your own device with Client Push through Back4App!

To learn more about Android push notification, see Parse Android Push Notification Official Documentation.