How to implement and test push notification in Cordova for android using local node server
Posted By : Akhil Dhiman | 24-Mar-2015
Here I will show you how one can setup push notification to android application.
Plugin to use:
I use the below plugin to enable push notification over server.
https://github.com/phonegap-build/PushPlugin
To add this plugin open terminal and execute below command:
cordova plugin add https://github.com/phonegap-build/PushPlugin.git
Once you setup with plugin then open below url and do the following :
https://console.developers.google.com
- Login with your credentials.
- Create an application.
- Get Project Number.
- API key
API key can be found under credentials tab of Api and auth. Each application has unique project number and api key.
How to setup push notification in device:
To enable push notification in device you need to write the following code snippet into device ready event.
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady(){
var pushNotification = window.plugins.pushNotification;
pushNotification.register(successHandler, errorHandler, {
"senderID" : "your application sender id",
"ecb" : "onNotificationGCM"});
}
function onNotificationGCM(e){
switch( e.event ) {
case 'registered':
if (e.regid.length > 0) {
console.log("Regid " + e.regid);// a unique device token needs to send it to server
alert('registration id = ' + e.regid);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = ' + e.message + ' msgcnt = ' + e.msgcnt);
break;
case 'error':
alert('GCM error = ' + e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
function successHandler(result) {
alert('Callback Success! Result = ' + result);
}
function errorHandler(error){
alert(error);
}
when device ready event fire then it will execute onNotificationGCM() function and for the first time it will register your device and fetch registration id and send that regid to the server. The regid will be unique for each and every device.
Test Push Notification with node server:
Firstly you need to get node-gcm package for that you need to run following command in terminal:
sudo npm install node-gcm
create a js file with name notify.js and write the following code snippet in it.
var gcm = require('node-gcm');
var message = new gcm.Message();
//API Server Key
var sender = new gcm.Sender('pass api key');//api key will be generated from developer account
var registrationIds = [];
var data = "hello how are you";
// Value the payload data to send...
message.addData('message',"\u270C Peace, Love Your Self"+data+" \u2764 and PhoneGap \u2706!");
message.addData('title','Push Notification Sample' );
message.addData('msgcnt','3'); // Shows up in the notification in the status bar
message.addData('soundname','beep.wav'); //Sound to play upon notification receipt - put in the www folder in app
//message.collapseKey = 'demo';
//message.delayWhileIdle = true; //Default is false
message.timeToLive = 3000;// Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
// At least one reg id required
registrationIds.push('pass reg id'); // registration id of device fetched by push plugin
sender.send(message, registrationIds, 4, function (result) {
console.log(result);
});
To send push notification combile node-gcm package and notify.js file and write the below line in terminal.
node notify.js
The Device will receive push notifiaction now.
THANKS
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
Akhil Dhiman
Akhil is an iPhone and Android application developer with experience in PhoneGap and Swift(Native iOS). Akhil has good experience working with JavaScript, jQuery and Underscore as well.