Hello All,
If you want to setup a push notifications with PhoneGap and Android, here below I am providing the guidelines, you only have to follow the steps one by one-
So first of all you have to install push plugin for PhoneGap.
The plugin can be installed via the Cordova command line interface give below-:
$ cordova plugin add https://github.com/phonegap-build/PushPlugin.git
You also need the PhoneGap device plugin:
$ cordova plugin add org.apache.cordova.device
Now copy pushNotification.js into www folder and Include this file in your apps index.html -file:
for example : <script src = "pushNotification.js></script>
Now you just need to Setup your app within Google Developer Console:
So first log-in Google Developer Console.
This Blog does not go into depth about creating accounts on Google Cloud Messaging, below link will provide you information about how to generate Project ID, Project Number and API Key-
http://developer.android.com/google/gcm/gs.html
Now we just need to add some JavaScript to allow our app to register the users device and to receive push notifications on it:
var pushNotification;
document.addEventListener( "deviceready", onDeviceReady, false );
function onDeviceReady() {
pushNotification = window.plugins.pushNotification;
if ( window.device.platform == 'android' || device.platform == 'Android' ){
// Register for Android:
pushNotification.register(
pushSuccessHandler,
pushErrorHandler, {
"senderID":"044354353455", // Project number from Google Developer Console
"ecb":"onNotificationGCM"
}
);
}
// Success handler for when connected to push server
function pushSuccessHandler(result) {
console.log(result);
}
// Error handler for when not connected to push server
function pushErrorHandler(error) {
console.log(error);
}
}
// Notification from Android GCM
var onNotificationGCM = function(e) {
// Check which event:
switch(e.event)
{
case 'registered' :
{
console.log('android reg id: ' + e.regid);
break;
}
}
};
0 Comment(s)