Android Push Notification Using Parse.Com

DZone 's Guide to

Android Push Notification Using Parse.Com

Master push notifications the easy way for your Android apps

· Mobile Zone ·
Free Resource

Android push notification is a service used to send messages directly to the Android smart phones. Using this service, developers can send data to Android apps as soon as it is available, in this way Android app doesn't have to make requests to the server to know if new information are available.

Using Android push service, apps can save smartphone battery and reduce the network traffic: the user experience is also improved. 

There are several different methods that can be used to implement Android push notification, the standard way is using GCM (Google Cloud Messaging) but there are some alternatives very interesting like Parse.com, that is easier to use.

Parse.Com Set Up The Project

The first thing is creating a Parse account and configure a new app. This is very easy. Once you have all things done, it is time to create a new project in Android Studio and modify build.grade to include the parse library: 

dependencies {
    compile 'com.parse.bolts:bolts-android:1.2.1'
    compile 'com.parse:parse-android:1.10.1'

Now you can follow, the tutorial provided by Parse.com. In our case, we have created a ParseTutorialApplication that extends Application and it is used to configure the Parse connection:

public class ParseTutorialApplication extends Application {
    public void onCreate() {
        Parse.initialize(this, "your key", "your key");

By now you use the default receiver provided by the library, in the next paragraph you will see how to customise it.

If the project is configured correctly, you can try to send a push notification using the web interface:


In your Android emulator, you should get the notification:


Please be sure that the emulator includes Google API.

Parse.Com Custom Receiver

Now it is time to customise the receiver so that we can support custom message and not only text messages. Customising the receiver, it is possible to implement the app logic like parsing the incoming message and show custom messages and so on. Looking back in the Manifest.xml, as broadcast receiver it was used the standard receiver com.parse.ParsePushBroadcastReceiver, now to customise its behaviour we can subclass it:

public class JSONCustomReceiver extends ParsePushBroadcastReceiver {
    protected void onPushReceive(Context context, Intent intent) {

and override the onPushReceiver so that it is possible to implement the app logic when the message is available. Let us suppose that the message is in JSON format like that:

{"message":"hello phone"}

In the onPushReceiver, the app parses the message: 

private String getData(String jsonData) {
        // Parse JSON Data
        try {
            System.out.println("JSON Data ["+jsonData+"]");
            JSONObject obj = new JSONObject(jsonData);
            return obj.getString("message");
        catch(JSONException jse) {
        return "";

Once the message content is available and extracted from JSON message, the app notifies it to the user using NotificationCompat and NotificationManager

// Create custom notification
NotificationCompat.Builder  builder = new NotificationCompat.Builder(context)
  .setContentTitle("Notification from Parse")
Notification notification = builder.build();
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

where pendingIntent is the instance of the Intent that starts the Activity when user touches the push notification: 

// Add custom intent
Intent cIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
                              cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

and finally the app notifies the message:

nm.notify(1410, notification);

The final result is shown in the picture below:


Notice we have customised the notification icon too.
At the end of this post you know how to send android push message using Parse.com, in the next posts you will learn how to use push notification to send messages generated by smart controllers like Arduino, stay tuned! 

android ,parse ,push notifications

Published at DZone with permission of Francesco Azzola , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}