Tuesday, June 11, 2013

Push Notifications in iOS Using Cordova + Java APNS

Below is an overview on implementing push notifications in a cordova (phonegap) ios application..

1. Setup the required provisioning profile for developing push notifications in member center of apple developer site.

2. Setup your Cordova iOS application

- Download the latest version of cordova from http://cordova.apache.org/#download (the version I used was 2.8).
- Unzip the downloaded cordova folder and follow the instructions in the README file of the cordova-ios folder to setup a cordova ios project. Be sure to set the project bundle id to be the same as the one you gave for the provisioning profile setup for push notifications.

3. Create a iOS plugin for enabling push notifications in cordova ( currently this functionality isn't built in to cordova)

Follow the tutorial http://cordova.apache.org/docs/en/edge/guide_plugin-development_index.md.html#Plugin%20Development%20Guide , to create an ios plugin. In the objective c plugin class, call the registration method for push notifications.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];

4. Setup the required methods in AppDelegate to handle the incoming notifications. See http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1

5. Create the push notification java server and invoke push notifications from it. I used java-apns library and followed this tutorial : http://developement.sebastian-bothe.de/?page_id=40.

Now you will have a complete setup for push notifications in iOS. Creating your own cordova plugin, instead of using an existing plugin gives you more control on how the push notifications will work in the app. Coming from an iOS development background, rather than a web development background, I found building my own objective c plugin to be easier and more flexible.