CordovaPush not working


#1

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);
    });
  });
});

#2

});undefined> }

???

Just to make sure … you are testing on a real device right?


#3

Thats correct. Error on app - cannot read property register of undefined


#4

Now I am getting different error
"Uncaught TypeError: Cannot read property ‘pushNotification’ of undefined", source: file:///android_asset/www/lib/ngCordova/dist/ng-cordova.js (6088)


#5

Is your ng-cordova.js file initiating before your cordova.js file?

Sounds like the push plugin is not loading, maybe try to remove the push plugin and add it back again.


#6

I have tried that but no luck! Yes ng-cordova.js is included before cordoa.js


#7

What If I just use what pushplugin has to offer? This way i can use native notifications. But I am not sure why it is not working with ngCordova :frowning:

As per docs