Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

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 {
 
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Application");
        Parse.initialize(this, "your key", "your key");
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }
}


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:



Schermata%2B2015-09-08%2Balle%2B22.20.20





In your Android emulator, you should get the notification:


device-2015-09-01-225504



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 {
    @Override
    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) {
            jse.printStackTrace();
        }
 
        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)
  .setSmallIcon(R.drawable.ic_not_alert)
  .setContentText(data)
  .setContentTitle("Notification from Parse")
  .setContentIntent(pendingIntent);
 
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:

device-2015-09-06-222013

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! 


Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:
parse ,android ,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.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}