Cordova Push - $cordovaPush is undefined for 'get' method

Hey guys,

Going nuts on that one. I keep getting that error when runing on my Android device:

.controller('InitialCtrl', function($rootScope, $scope, $cordovaPush, $cordovaDialogs, $cordovaMedia, $cordovaToast, $ionicPopup, $ionicPlatform) {
    processMessage failed: Error: TypeError: Cannot read property 'get' of undefined
        processMessage failed: Stack: TypeError: Cannot read property 'get' of undefined
            at eval (eval at processMessage (file:///android_asset/www/cordova.js:1021:26), <anonymous>:1:74)
            at processMessage (file:///android_asset/www/cordova.js:1021:13)
            at Function.androidExec.processMessages (file:///android_asset/www/cordova.js:1091:13)
            at pollOnce (file:///android_asset/www/cordova.js:956:17)
            at pollOnceFromOnlineEvent (file:///android_asset/www/cordova.js:946:5)
        processMessage failed: Message: Jjavascript:angular.element(document.querySelector('[ng-app]')).injector().get('$cordovaPush').onNotification({"event":"registered","regid":"APA91bFCuwFAveicLbPtR[...]"})

Where is that coming from?? ngCordova is loaded and here is the begining of my code :
Thanks for your help!!

    $scope.notifications = [];
      // call to register automatically upon device ready
      $ionicPlatform.ready(function (device) {
        var config = null;
    
        if (ionic.Platform.isAndroid()) {
            console.log('I am Android');
            config = {
                "senderID": "12345677 my id" // PROJECT ID FROM GCM CONSOLE 
            };
        }
        else if (ionic.Platform.isIOS()) {
            console.log('I am iOS');
            config = {
                "badge": "true",
                "sound": "true",
                "alert": "true"
            }
        }
        
        $cordovaPush.register(config).then(function (result) {
              console.log("Register success " + result);
              
              // ** Can be removed after test
              $cordovaToast.showShortCenter('Registered for push notifications');
              $scope.registerDisabled=true;
              // ** NOTE: Android regid result comes back in the pushNotificationReceived, only iOS returned here
              if (ionic.Platform.isIOS()) {
                  $scope.regId = result;
                  storeDeviceToken("ios");
              }
          }, function (err) {
              console.log("Register error " + err)
          });
      });
    
      // Notification Received
      $rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
          console.log(JSON.stringify([notification]));
          if (ionic.Platform.isAndroid()) {
              handleAndroid(notification);
          }
          else if (ionic.Platform.isIOS()) {
              handleIOS(notification);
              $scope.$apply(function () {
                  $scope.notifications.push(JSON.stringify(notification.alert));
              })
          }
      });
})

Found something!
It is linked to the fact that I removed the ng-app to bootstrap manually the app… now I need to figure a way to combine everything…

I am struggling with the exact same issue today. Have you found a solution?

Not yet I am going crazy…
There is a github issue openened also, guy gives a trick but it doesn’t work for me to set the "ecb"

did you find something or a way around?

Naah, had been struggling for 2 hrs when I googled and found your post. I’ll give it a show tomorrow and hopefully find a solution. I’ll make sure to post my findings!

Same will let you know!
So frustrating it’s the last piece I need to do to finish :smile:

OK I got a dodgy fix while it gets officialy corrected
File ng-cordova.js around line 5123 in the register function I replaced existing by:

if (config !== undefined && config.ecb === undefined) {
          console.log(config.ecb)
          if (angular.element(document.querySelector('[ng-app]')) === undefined) {
            console.log('I am not defined')
            injector = "document.body";
          }
          else {
            console.log('ng-app defined')
            // HACKED IT TO WORK WHILE WAITING FOR A FIX. IF SHOULD NOT ARRIVE HERE BUT GO TO 'I AM NOT DEFINED' SINCE
            // APP HAS BEEN LOADED WITH angular.bootstrap AND NOT ng-app - SO IT SHOULDN'T EVEN SAY THAT IT IS DEFINED... WEIRD!
            // UPDATE THE .min.js VERSION AN REPORT ISSUE 
            injector = "document.body";
          }
          config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification";
        }

Why? Because the if call is weird in my app, it resturns that the ng-app is defined but it is not… so I forced it to take document.body and it works.

Happy thursday! :smiley: :sunrise_over_mountains:

Nice. We actually found a solution that did NOT require a hack which might interest you :smile:

Just set ecb in your config like this:

"ecb": "angular.element(document.body).injector().get('$cordovaPush').onNotification" 

No hack :smile:

3 Likes

haha awesome, :smiley: I prefer your solution than modifying the ng-cordova files :smile: I will implement it tonight.

Thanks!

Thanks @jenssog. It is a clean solution. :smile: