I want to use native notifications and I have setup Android and iOS push notifications according to ionic documentation
I have added phone gap push plugin and ngcordova. The ngcordova.js has been imported before cordova.js I am getting $window.plugin not defined error in browser which I believe is expected. But I am getting cannot read property register of undefined error in app which means $cordovaPush is undefined.
angular .module('init') .run(function($ionicPlatform, $rootScope, $state, $cordovaPush, $cordovaToast){ $ionicPlatform.ready(function() { // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard // for form inputs) if(window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); } if(window.StatusBar) { window.StatusBar.styleDefault(); } registerForPush(); function registerForPush(){ try { var config = {}; //register for push notifications if (ionic.Platform.isAndroid()) { config = { "senderID": "1232" }; }else if (ionic.Platform.isIOS()) { config = { "badge": "true", "sound": "true", "alert": "true" } } $cordovaPush.register(config).then(function (result) { alert("Register success " + result); $cordovaToast.showShortCenter('Registered for push notifications'); // ** NOTE: Android regid result comes back in the pushNotificationReceived, only iOS returned here if (ionic.Platform.isIOS()) { //register } }, function (err) { console.log("Register error " + err) });
} catch(err) { alert(err.message); } } // Notification Received $rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) { alert(JSON.stringify([notification])); if (ionic.Platform.isAndroid()) { if (notification.event == "registered") { alert("android notification received token - " + notification.regid); } } else if (ionic.Platform.isIOS()) { alert("ios notification received"); } }); document.addEventListener('resume', function () { $rootScope.$broadcast('resume'); }); document.addEventListener('pause', function () { $rootScope.$broadcast('pause'); }); document.addEventListener('offline', function () { $rootScope.$broadcast('offline'); }); document.addEventListener('online', function () { $rootScope.$broadcast('online'); }); window.addEventListener('batterystatus', function (status) { $rootScope.$broadcast('batterystatus', status); }); }); });