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:
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.
- 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.
- Follow the Steps 1 to 5 of Back4App Push Notifications via Dashboard tutorial carefully to set up Push Notifications to your app.
- A device (or virtual device) running Android 4.0 (Ice Cream Sandwich) or newer.
Step 1 - Enable Client Push
- Go to Back4App Website, log in, find your app and click on
Server Settings
. - Find the “Core Settings” block and click on
SETTINGS
. The “Core Settings” block looks like this: - Scroll to the end of the page and click on the
EDIT DETAILS
button, as shown below: - You will see a checkbox called
Allow Push Notification from Client
in the end of the edit page, tick that box and click on theSAVE
button, as shown below:
Step 2 - Push from your app
- 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;
- 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();
- 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.