Android push notification using Parse
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 set up 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:
In your Android emulator, you should get the notification:
Please be sure that the emulator includes Google API.
Parse custom receiver: JSON data
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:
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!
Reference: | Android push notification using Parse from our JCG partner Francesco Azzola at the Surviving w/ Android blog. |