Android push notification tutorial using Parse.com

Google Android push notification is a service used by developers to send data to Android apps as soon as the data 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 messages, the standard way is using GCM (Google Cloud Messaging) but there are some alternatives very interesting like Parse.com, that is easier to use.

Send push notification to android phone with parse.com

 

Android push notification example: Set up project with Parse.com

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 android push notification using the web interface:

 

android push text message

In your Android emulator, you should get the push notification:

android push notification with text message

 

Please be sure that the emulator includes Google API.

Custom receiver example : Receive JSON data

Now it is time to customise the receiver so that we can support custom as android push notification message and not only text messages. Customising the receiver, it is possible to implement custom 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:

Android push notification son message with custom receiver

Notice we have customised the notification icon too.
At the end of this post you know how push notification works and how to implement push notification android app using Parse.com.
In the next posts you will learn how to use push notification generated by smart controllers like Arduino!
Source code available @ github