I have an android app built on apache cordova. I want push notifications for this application. I have used Visual studio 2015 IDE to built this application. I have already installed some plugins for this application like cordova-plugin-device-orientation, statusbar etc. To enable push notifications for this app I have installed phonegap-plugin-push (version 2.1.0) and provided the sender Id for my application which I created using Firebase. By creating new project, added android app, downloaded google-services.json file and placed it in my project.
<script>
function setupPush() {
//This line below throws error on simulator saying PushNotification //is not defined
var push = PushNotification.init({
"android": {
"senderID": "XXXXXXXXXX"
},
"ios": {
"sound": true,
"alert": true,
"badge": true
},
"windows": {}
});
alert('registered');
push.on('registration', function (data) {
alert('registration event' + data.registrationId);
console.log("registration event: " + data.registrationId);
var oldRegId = localStorage.getItem('registrationId');
if (oldRegId !== data.registrationId) {
// Save new registration ID
alert('registration event' + data.registrationId)
localStorage.setItem('registrationId', data.registrationId);
// Post registrationId to your app server as the value has changed
}
});
push.on('error', function (e) {
console.log("push error = " + e.message);
});
}
$(document).on('deviceready', function deviceIsReady() {
console.log('Device is ready!');
setupPush();
});
</script>
I found this code here.