Can't get push notifications in Android to work using ngCordova

I’m having a tough time getting push notifications (using the ngCordova plugin) to work. I have followed their sample code exactly as is documented on the site: http://ngcordova.com/docs/plugins/pushNotifications/

(the only difference is that I don’t have a deviceready listener, instead, my code is inside the ionicPlatform.ready listener.)

Here is my code:

angular.module('myApp', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $rootScope, $state, $cordovaPush) {
  $ionicPlatform.ready(function() {
    var config = {
      "senderID": "myID100001000"
    };

    $cordovaPush.register(config).then(function(result) {
      alert(result);
    }, function(err) {
      alert(err);
    })      
  }); 

  $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
    switch(notification.event) {
      case 'registered':
        if (notification.regid.length > 0 ) {
          alert('registration ID = ' + notification.regid);
        }
        break;

      default:
        alert('An unknown GCM event has occurred');
        break;
    }
  });  
})

When my app starts I do get the “OK” alert, so I know it successfully goes through the $cordovaPush.register call. However, I was expecting to get a “registered” notification event, right after, but I never get notified.

Any help would be appreciated.

Hi,

I’m having the same problem as you. My code is pretty much the same (as I also followed the examples online). Did you manage to find a solution? Or can anyone else help us out here?

Have you tried adding “ecb” to config? So something liek that:

var config = {
  "senderID": "myID100001000",
  "ecb":"angular.element(document.querySelector('[ng-app]')).injector().get('NotificationService').notify"
};

Thanks for the reply! After some more intense searching I’ve also stumbled across posts with similar suggestions to that. e.g. Cordova Push - $cordovaPush is undefined for ‘get’ method

I will try when I get home tonight and report back. Thanks again

Turns out it was my mistake all along. Typical! I swear I had tried this last night but $rootScope was not down to be injected like in the example above. Perhaps when I had tried it with it in there was some other JS error. What helped narrow this down straight away was USB debugging for the device (which I should have done in the first place) https://developer.chrome.com/devtools/docs/remote-debugging