Posts Tagged ‘phonegap’

Pushing the Phone Gap

Posted: January 29, 2015 in Kreeps
Tags: , ,

The days when developers had to develop separate apps for each target platform are nearing its end. Thanks to technologies like phonegap. It allows us to code your app just once and deploy over various platforms.This post is just a log for the information I came across working with push notifications for PhoneGap app. PhoneGap reduces a lot of effort when your aim is to develop an for various application environments (iOS, android, windows…). You don’t have to code separately for each target platform. Though phonegap docs show a lengthy procedure to set up your development environment, I found the following video on YouTube more useful and less painstaking:

https://www.youtube.com/watch?v=0gGBhaVG9CI

App development is done in Html. If UI is not of a very major concern, jQueryMobile is a nice javascript library which will help you with your UI. Since, the app made using phonegap runs on a browser, there are obviously some limitations. I am not going to detail them here.

PhoneGap’s latest PushPlugin helps us to add push notification capabilities to our app. Following git hub links explain how to use the push notification:

https://github.com/phonegap-build/PushPlugin

The PushNotification.js file given in this repository has some missing semicolons. You can instead paste following corrected code.



var PushNotification = function() {
};

// Call this to register for push notifications. Content of [options] depends on whether we are working with APNS (iOS) or GCM (Android)
PushNotification.prototype.register = function(successCallback, errorCallback, options) {
if (errorCallback == null) { errorCallback = function() {};}

if (typeof errorCallback != “function”) {
console.log(“PushNotification.register failure: failure parameter not a function”);
return;
}

if (typeof successCallback != “function”) {
console.log(“PushNotification.register failure: success callback parameter must be a function”);
return;
}

cordova.exec(successCallback, errorCallback, “PushPlugin”, “register”, [options]);
};

// Call this to unregister for push notifications
PushNotification.prototype.unregister = function(successCallback, errorCallback) {
if (errorCallback == null) { errorCallback = function() {};}

if (typeof errorCallback != “function”) {
console.log(“PushNotification.unregister failure: failure parameter not a function”);
return;
}

if (typeof successCallback != “function”) {
console.log(“PushNotification.unregister failure: success callback parameter must be a function”);
return;
}

cordova.exec(successCallback, errorCallback, “PushPlugin”, “unregister”, []);
};

// Call this to set the application icon badge
PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallback, errorCallback, badge) {
if (errorCallback == null) { errorCallback = function() {};}

if (typeof errorCallback != “function”) {
console.log(“PushNotification.setApplicationIconBadgeNumber failure: failure parameter not a function”);
return;
}

if (typeof successCallback != “function”) {
console.log(“PushNotification.setApplicationIconBadgeNumber failure: success callback parameter must be a function”);
return;
}

cordova.exec(successCallback, errorCallback, “PushPlugin”, “setApplicationIconBadgeNumber”, [{badge: badge}]);
};

//——————————————————————-

if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.pushNotification) {
window.plugins.pushNotification = new PushNotification();
}

if (module.exports) {
module.exports = PushNotification;
}


For setting up GCM follow this: http://apigee.com/docs/app-services/content/registering-notification-service

For using php to send push notifications: http://distriqt.com/post/1273

The following link helps in adding and testing push notification in app : http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

Advertisements