Search code examples
cordovaionic-frameworkionic-view

ionic setting up push - Cannot read property of undefined


I'm working on a push app via ionic. I followed this tutorial from ionic itself: http://docs.ionic.io/docs/push-from-scratch

Registering users - Works perfect! Setting up Push notifications don't

It actually worked perfectly two weeks ago, was able to do everything I wanted. I expect some breaking change in ngCordova/ionic but I can't find it.

The error

Cannot read property 'pushNotification' of undefined

at line:         $ionicPush.register({ 

The trace

TypeError: Cannot read property 'pushNotification' of undefined
    at Object.register (ng-cordova.js:6362)
    at init (ionic-push.js:146)
    at ionic-push.js:309
    at new Q (ionic.bundle.js:22259)
    at Q (ionic.bundle.js:22246)
    at Object.register (ionic-push.js:270)
    at Scope.$scope.pushRegister (controllers.js:60)
    at $parseFunctionCall (ionic.bundle.js:21172)
    at ionic.bundle.js:53674
    at Scope.$get.Scope.$eval (ionic.bundle.js:23228)

And the code (controllers.js)

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope, $rootScope, $ionicUser, $ionicPush) {


  // // Handles incoming device tokens
  $rootScope.$on('$cordovaPush:tokenReceived', function(event, data) {
    alert("Successfully registered token " + data.token);
    console.log('Ionic Push: Got token ', data.token, data.platform);
    $scope.token = data.token;
  });

  // // Identifies a user with the Ionic User service
  $scope.identifyUser = function() {
    console.log('Ionic User: Identifying with Ionic User service');

    var user = $ionicUser.get();
    if(!user.user_id) {
      // Set your user_id here, or generate a random one.
      user.user_id = $ionicUser.generateGUID();
    };

    // Add some metadata to your user object.
    angular.extend(user, {
      name: 'Ionitron',
      bio: 'I come from planet Ion'
    });

    // Identify your user with the Ionic User Service
    $ionicUser.identify(user).then(function(){
      $scope.identified = true;
      alert('Identified user ' + user.name + '\n ID ' + user.user_id);
    });
  };

   $scope.pushRegister = function() {
    console.log('Ionic Push: Registering user');
    alert('bla');
    // Register with the Ionic Push service.  All parameters are optional.
    $ionicPush.register({ //ERROR FIRES HERE
      canShowAlert: true, //Can pushes show an alert on your screen?
      canSetBadge: true, //Can pushes update app icon badges?
      canPlaySound: true, //Can notifications play a sound?
      canRunActionsOnWake: true, //Can run actions outside the app,
      onNotification: function(notification) {
        // Handle new push notifications here
        // console.log(notification);
        return true;
      }
    });
    alert('bla2');
  };
})

I updated cordova, added and removed ionic ready listener. I tried running it on phone which gave this error: enter image description here

Do you have any suggestions?


Solution

  • Try ionic config set dev_push true in the terminal.

    I posted about this here: https://github.com/driftyco/ionic-push-tutorial-app/issues/7