Push Notifications using Google Cloud Messaging in Android Part II
Posted By : Varun Sharma | 30-Jul-2012
Before reading Part 2 plese refer Part 1
Push Notifications using Google Cloud Messaging (GCM) in Android (Part 1)
Before going for Server Side & Client Side development, you need
1. Sender ID
2. API Key and
3. Registration ID
For the Sender ID & API Key you can refer This Link. Hope you go through the link & got your Sender ID (or Project ID) & API Key.
Now you have Sender ID & API Key so lets move to your application & lets start with Client Side to register the device for receiving GCM messages.
Client Side (Titanium)
1. Download C2DM module from This Link
2. Copy c2dm.jar file from dist folder and goto /Users/Username/Library/Application Support/Titanium/modules/ now create com.findlaw.c2dm > 0.1 folder and paste c2dm.jar file into it.
3. Now attach the module to your project.
Note: This Module only works in Rhino runtime.
Your app.js should be like
var senderId = 'XXXXXXXXXX'; var c2dm = require('com.findlaw.c2dm'); Ti.API.info("module is => " + c2dm); Ti.API.info('Registering...'); c2dm.registerC2dm(senderId, { success:function(e) { Ti.API.info('JS registration success event: ' + e.registrationId); }, error:function(e) { alert(e.error); }, callback:function(e) // called when a push notification is received { Ti.API.info('JS message event: ' + JSON.stringify(e.data)); var intent = Ti.Android.createIntent({ action: Ti.Android.ACTION_MAIN, flags: Ti.Android.FLAG_ACTIVITY_NEW_TASK | Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED, className: 'com.company.app.YourActivity', packageName: 'com.company.app' }); intent.addCategory(Ti.Android.CATEGORY_LAUNCHER); // This is fairly static: Not much need to be altered here var pending = Ti.Android.createPendingIntent({ activity: Ti.Android.currentActivity, intent: intent, type: Ti.Android.PENDING_INTENT_FOR_ACTIVITY, }); var notification = Ti.Android.createNotification({ contentIntent: pending, contentTitle: 'New message', contentText: e.data.message, tickerText: "New message" }); Ti.Android.NotificationManager.notify(1, notification); } });
Your tiapp.xml should be like
com.findlaw.c2dm
Now you got your Registration ID, this will be used at Server Side to send Push Notifications to devices. So lets start with Server Side to send Push Notifications to devices.
Server Side (Grails)
1. Add the gcm.jar & gcm-server.jar from the Android SDK extras folder to Grails build path.(From android-sdk-linux/extras/google directory to your application classpath as external jar)
Use the below code to send notifications
ArrayListdevices = new ArrayList (); devices.add("REG_ID_1"); devices.add("REG_ID_2"); Sender sender = new Sender("YOUR_API_KEY"); Message msg = new Message.Builder() .collapseKey("1") .timeToLive(3) .delayWhileIdle(true) .addData("message",”This is a notification”) .build(); try { MulticastResult result = sender.send(msg, devices, 5); for (Result r : result.getResults()) { if (r.getMessageId() != null) { String canonicalRegId = r.getCanonicalRegistrationId(); if (canonicalRegId != null) { // same device has more than on registration ID: update database // BUT WHICH DEVICE IS IT? } } else { String error = r.getErrorCodeName(); if (error.equals(Constants.ERROR_NOT_REGISTERED)) { // application has been removed from device - unregister database // BUT WHICH DEVICE IS IT? } } } } catch (IOException e) { log.debug "Exception: "+e }
That's all, you are done. You will soon get a Push Notification on your Android Device.
>Hope it helps !
Varun Sharma [email protected]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Varun Sharma
Varun is an experienced Groovy and Grails developer and has worked extensively on designing and developing applications with FaceBook , Linkedin and Twitter integrations using Grails technologies. Varun loves painting and photography.