iOS Push Notification SDK for Cordova

To integrate Pushwoosh with your PhoneGap Cordova application you need to do simple following steps:

1. Get the Plugin source code from: https://github.com/shaders/phonegap-cordova-push-notifications/tree/master/iOSs
or use PhoneGap Cordova sample application: https://github.com/shaders/push-notifications-sdk/tree/master/SDK%20Sample%20Projects/iPhone-Phonegap

2. Add Plugins folder to your project in XCode (use “Create groups for any added folders”)

3. Copy PushNotification.js file from the “www” folder to your “www” folder in the project

4. Add the reference to the .js file using <script> tags in your html file(s):

<script type="text/javascript" charset="utf-8" src="PushNotification.js"></script>

5. Let Cordova know about push notifications plugin by adding plugin entry to the “config.xml” under plugins tag:

	<plugin name="PushNotification" value="PushNotification" />

Whitelist *.pushwoosh.com domain in the same file under access tag:

	<access origin="*.pushwoosh.com" />

6. Registering for push notifications:
Add the following function to your javascript file, enter the correct Pushwoosh App ID and your App title.

function initPushwoosh() {
	var pushNotification = window.plugins.pushNotification;
	pushNotification.onDeviceReady();

	pushNotification.registerDevice({alert:true, badge:true, sound:true, pw_appid:"PUSHWOOSH_APP_ID", appname:"APP_NAME"},
		function(status) {
			var deviceToken = status['deviceToken'];
			console.warn('registerDevice: ' + deviceToken);
		},
		function(status) {
			console.warn('failed to register : ' + JSON.stringify(status));
			navigator.notification.alert(JSON.stringify(['failed to register ', status]));
		}
	);

	pushNotification.setApplicationIconBadgeNumber(0);

	document.addEventListener('push-notification', function(event) {
		var notification = event.notification;
		navigator.notification.alert(notification.aps.alert);
		pushNotification.setApplicationIconBadgeNumber(0);
	});
}

In your onDeviceReady function add:
initPushwoosh();

Example:

    bind: function() {
        document.addEventListener('deviceready', this.deviceready, false);
    },
    deviceready: function() {
        // note that this is an event handler so the scope is that of the event
        // so we need to call app.report(), and not this.report()
        initPushwoosh();

        app.report('deviceready');
    },

7. Receiving push notifications. See the following snippet of code in initPushwoosh function

document.addEventListener('push-notification', function(event) {
	var notification = event.notification;
	navigator.notification.alert(notification.aps.alert);
	pushNotification.setApplicationIconBadgeNumber(0);
});

Wasn’t it too easy?

Push Notifications Plugin API:

    //access to the Push Notification Plugin
    var pushNotification = window.plugins.pushNotification;

	// Call this to register for push notifications and retrieve a deviceToken
	PushNotification.prototype.registerDevice = function(config, success, fail) {
		cordova.exec(success, fail, "PushNotification", "registerDevice", config ? [config] : []);
	};

	// Call this to set tags for the device
	PushNotification.prototype.setTags = function(config, success, fail) {
		cordova.exec(success, fail, "PushNotification", "setTags", config ? [config] : []);
	};

	// Call this to send geo location for the device
	PushNotification.prototype.sendLocation = function(config, success, fail) {
		cordova.exec(success, fail, "PushNotification", "sendLocation", config ? [config] : []);
	};

	PushNotification.prototype.onDeviceReady = function() {
		cordova.exec(null, null, "PushNotification", "onDeviceReady", []);
	};

	// Call this to get a detailed status of remoteNotifications
	PushNotification.prototype.getRemoteNotificationStatus = function(callback) {
		cordova.exec(callback, callback, "PushNotification", "getRemoteNotificationStatus", []);
	};

	// Call this to set the application icon badge
	PushNotification.prototype.setApplicationIconBadgeNumber = function(badge, callback) {
		cordova.exec(callback, callback, "PushNotification", "setApplicationIconBadgeNumber", [{badge: badge}]);
	};

	// Call this to clear all notifications from the notification center
	PushNotification.prototype.cancelAllLocalNotifications = function(callback) {
		cordova.exec(callback, callback, "PushNotification", "cancelAllLocalNotifications", []);
	};

Sample push notification payload:

{
    "aps": {
        "sound": "default",
        "alert": "push title"
    },
   u: '{key: value}'	//user data
}