Recently we were working on android application and we had requirements from our client to support push notification. We already had implemented Apple Push Notification Service for the iPhone application and thought there would be similar solution for android push too. As android platform doesn’t have single vendor like Apple so we were wondering who would support it. There are many custom build solutions based on application polling or asynchronous connection to server but timing of these event was tricky part plug they is additional usage of network resource and battery.
Welcome to the world of ‘Cloud to Device Messaging’ or C2DM supported by google services. This solution provides a solution to send real time notification to the user on user’s handset. Since Google already uses its service for Calendar or gmail etc. on the android platform so there won’t be extra network usage or battery usage for c2dm.
There are plenty of excellent articles which explain working on C2DM push notification and provide sample code to Run. We feel some points were missing or not obvious in those articles specially when you are not aware of Push Notification concept. Also there is no default notification implementation on Android platform. So we decided to fill in the gap and allow developers to quickly run a sample android push application and recieve the push notification from third party server.
For this purpose, we created a sample application which registers the device to google service and send the registrationId to the our own third party server. The sample application displays the notification screen whenever a push notification is received by android application. You can find many server-side solutions for scripting language like php,shell script using curl etc. Since most of the enterprise applications run on J2EE platform so we decided to have a J2EE server-side solution. In our server-side implementation, you can regsiter device with registerId, view list of registerId and can send sample push to the client.
So the purpose of this article is to understand android push notification(c2dm) and start working on it quickly..Using this article developer can register the application from device and can send notification from web-interface. This way developer can also display push notification to clients or non-technical person.
There are three entity involved in android push notification:
- Android application – registers the device with C2DM server and passes the registerId to third party server
- Google’s C2DM servers – send messages to android device
- Third Party Server – send messages to C2DM server
Sample Application for Android Push Notification (C2DM):
We have taken the source code from cw-advandroid and added few tweaks in the source code. The only thing you need to do is to download the code from below link and configure it in your favorite IDE (We would prefer to use eclipse though). You need to open strings.xml file inside res/values directory and change the server URL.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">PushEndpointDemo</string> <string name="server_url">http://192.168.1.4:8080/c2dm/notify</string> </resources>
If you are deploying the server application (We shall discuss server-side in next section) on local machine then replace the ipaddress with your machine’s ipAddress. Now your application is ready to register your app with google and send the registrationId to your third party server.
Please notice few points before running the app on android:
- You must have android sdk 2.2+ and google service must be running on your emulator (We would recommend to install google sdk 2.3.1)
- You must register your Google account on your emulator. You can do it by going to Settings -> Accounts & Sync and add gmail account. You cannot receive push notification unless Google account is set.
- You must register your developer’s email address for your application at google . Please note one thing that this account is developer’s account and might not be account which is configured in ‘Accounts & Sync’ settings.
Third party server to register device and send Android Push Notification (C2DM):
We have built server-side solution on J2EE platform and application can be deployed on tomcat server(It might deployed fine on other servers but hav’nt tested it). Before deploying to server, you need to set the authentication code for your developer account in the application so please set it in the AUTH_KEY variable in class com.myor.c2dm.utils.Constants.java
public static final String AUTH_KEY = "" //Please set your authentication key
You can get authentication key using curl as mentioned this article or You can use the our utility com.myor.c2dm.utils.AuthenticationUtils
Please use maven to create war file and deploy it on your server web directory.
mvn clean package
You can access the application at
Now we ready to register and receive the android push notification on sample application. Please launch the application on your emulator or device and screen would be displayed like this:
Enter your developer email account and click on the ‘Register!’ button. This shall make a call to google service to register the device for application. In the callback method, we make call to our third party server to register the registrationId for the device. We can access the registerId at
Now we can send push notification to the android application by clicking the ‘Push’ link. The notification on the client–side look shall like this:
When you drag down the notification window then full notification shall be displayed like this:
We hope this article shall help you to power your application with Android push notification. Feel free to give us feedbacks or let us know if you have any queries.