ngCordova $cordovaPush and Push Notifications

Let me see if i get you. Is the callback of onRegister() never being executed or are you getting ‘ok’ in it but not the actual device info?

It’s the callback that never gets called.

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

The above executes fine, my success handler does get called, and my alert shows OK.

However, this callback never gets called, but this is what I really need to get back my registration id:

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

can you look through your dom and see if the ecb onnotification handler is being added - this is done via the ngCordova push code

  .factory('$cordovaPush', ['$q', '$window', '$rootScope', '$timeout', function ($q, $window, $rootScope, $timeout) {
return {
  onNotification: function (notification) {
    $timeout(function () {
      $rootScope.$broadcast('$cordovaPush:notificationReceived', notification);
    });
  },

  register: function (config) {
    var q = $q.defer();
    var injector;
    if (config !== undefined && config.ecb === undefined) {
      if (document.querySelector('[ng-app]') == null) {
        injector = "document.body";
      }
      else {
        injector = "document.querySelector('[ng-app]')";
      }
      config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification";
    }

    $window.plugins.pushNotification.register(function (token) {
      q.resolve(token);
    }, function (error) {
      q.reject(error);
    }, config);

    return q.promise;
  },

also I assume your Google senderID - is registered to enable the GCM push - this is done via the google developer console from memory

Hmm, I can’t find this in the dom. Is this supposed to be in the index.html source? Other than my own code, I only have the liveReloadPlugin code injected into the source. Also, ngCordova plugins have never worked for me in the browser, so should I still expect to see it in the dom on the browser?

Thanks for your time…

no worries

the plugin wont work in a browser (as in register for or receive pushes) but you should be able to debug and step through the register code above to ensure that the ecb is being injected ok.
If it is then my next guess would be that the Plugin registration process is not completing and no notification is being sent back. This could be that your senderID (from you goggle developer console) is not right or that it is not enable for the GCM API - sorry can’t recall the exact words.

I don’t use the ngCordova modules for Push. I use the calls and methods described in the plugin itself - so sorry if not able to help enough

also check out this article and this piece of code - looks like it is looking for a difference event - see the $on

hope this gets you going

blog - http://devgirl.org/2014/12/16/push-notifications-sample-app-with-ionic-and-ngcordova/

code - https://github.com/hollyschinsky/PushNotificationSample/blob/master/www/js/controllers.js

it looks like the ngCordova push plugin has changed its event name in the latest code - so the examples above are for a couple of versions of ngCordova ago. will depend on which version you have downloaded

I was able to locate that code when debugging, it was in ng-cordova.min.js, looks a little different than yours, but similar:

angular.module("ngCordova.plugins.push", []).factory("$cordovaPush", ["$q", "$window", "$rootScope", function(e, n, r) {
        return {
            onNotification: function(e) {
                r.$apply(function() {
                    r.$broadcast("pushNotificationReceived", e)
                })
            },
            register: function(r) {
                var o = e.defer();
                return void 0 !== r && void 0 === r.ecb && (r.ecb = "angular.element(document.querySelector('[ng-app]')).injector().get('$cordovaPush').onNotification"), n.plugins.pushNotification.register(function(e) {
                    o.resolve(e)
                }, function(e) {
                    o.reject(e)
                }, r), o.promise
            },
            unregister: function(r) {
                var o = e.defer();
                return n.plugins.pushNotification.unregister(function(e) {
                    o.resolve(e)
                }, function(e) {
                    o.reject(e)
                }, r), o.promise
            },
            setBadgeNumber: function(r) {
                var o = e.defer();
                return n.plugins.pushNotification.setApplicationIconBadgeNumber(function(e) {
                    o.resolve(e)
                }, function(e) {
                    o.reject(e)
                }, r), o.promise
            }
        }

I actually went through those articles you linked, and tried both $cordovaPush:notificationReceived and pushNotificationReceived. It seems like in the latest version of ngCordova, they have changed the event name to $cordovaPush:notificationReceived, but it looks like from my code, it’s the old on, pushNotificationReceived. I had tried both earlier and still couldn’t get it to work, but I’ll give it another shot now.

Also, yes, I did set up everything in GCM, I think I did it correctly, but I’ll check again. Do you know if the Google developer console would list the registration ids anywhere if the registration goes through successfully? I couldn’t find anything in the logs.

Hey, I got it to work! Something stupid as usual, and I don’t think any of the articles I went through mentioned this. But when I went to the GCM console to check if I had set everything up right, I noticed that there is a project Id and a project number. And the senderID is actually the project number, not the project ID, and I was using the project ID. Sure enough, once I used the project number, everything works as expected.

Thanks for your help, again!

Hi,

How can i get the message and save it locally on sqlite or something when the app is in background and receiving notifications.

that didn’t work for me.
I changed:

 $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {

to

$scope.$on('pushNotificationReceived', function (event, notification) {

only now I got it to work. and only after debugging ng-cordova.js

Hello, i install Push plugin and include at index.html file by line

and app error. Not found PushNotification.js

that is because you didn’t tell the controller you are using $rootScope as a dependency!

It is recommended to install it via Cordova CLI like so

cordova plugin add https://github.com/phonegap-build/PushPlugin.git

This will avoid those kind of errors

While that could be part of it, the big problem here was that the user was using an old version of ngCordova, where ‘pushNotificationReceived’ was used instead of ‘$cordovaPush:notificationReceived’.

I follow step by step this tutorial

I have 2 buttons, when I press the second one in console I have this error
TypeError: Cannot read property ‘pushNotification’ of undefined

Thank @yoavk,

I got the same issue and your solution is fine for me :slight_smile:

Yeah … did you figured this out? I need to know this for Android

I am having the same issue, already tried all the solutions mentioned above but still getting.

Uncaught TypeError: Cannot read property ‘pushNotification’ of undefined

Issue is with $window.plugins, it is undefined thats why $window.plugins.pushNotification this does not work.

But the question is why this is not defined, even if I am using a device and not browser.

When I add a new plugin to a ionic project, it look like the new plugin isn’t added to iOS project even if I build ionic the project. So,I always use the following commands to reset the plugin.

Reset plugins
ionic state reset --plugins

Add platform
ionic platform add ios

Build
ionic build ios

1 Like