In my ionic application when running the application in browser i get following error
ng-cordova.js:6378 Uncaught ReferenceError: PushNotification is not defined
in my console, as well as when i build the application for android and run in my phone it doesnot work there as well i.e there is no popup
for registration Id : alert(data.registrationId);
.
app.run(function($ionicPlatform, $cordovaPushV5) {
$ionicPlatform.ready(function() {
// For Push Notification
var options = {
android: {
senderID: "121XXXXXXX49"
},
ios: {
alert: "true",
badge: "true",
sound: "true"
}
};
// initialize
$cordovaPushV5.initialize(options).then(function() {
// start listening for new notifications
$cordovaPushV5.onNotification();
// start listening for errors
$cordovaPushV5.onError();
// register to get registrationId
$cordovaPushV5.register().then(function(data) {
// `data.registrationId` save it somewhere;
alert(data.registrationId);
})
});
// triggered every time notification received
$rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data){
alert(data.title + ' - '+ data.message);
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
});
// triggered every time error occurs
$rootScope.$on('$cordovaPushV5:errorOcurred', function(event, e){
alert(e.message);
// e.message
});
});
})
If you look at ngCordova page it is said "Cordova plugins do not work while developing in your browser". This is the reason why it is not working in the browser.
See the link here: http://ngcordova.com/docs/common-issues/
Before initializing the plugin you should check if app is running on device (not in browser) and ionic platform is ready. Then if you follow the link properly you are good to go.
The link: https://github.com/phonegap/phonegap-plugin-push
My personal advice is you don't have to use ng-cordova since new phonegap-push plugin is out there. You don't need to use any other things. You can basically do the following without using ng-cordova.
Make sure you check if ionic platform is ready and the platform is not browser.
var pushConfig = {
android: {
senderID: "blabla"
},
ios: {
alert: true,
badge: true,
sound: true
}
};
var push = window.PushNotification.init(pushConfig);
push.on('registration', function(data) {
var token = data.registrationId
console.log('OK: register notfy ', token);
});
Detailed information is here: https://github.com/phonegap/phonegap-plugin-push