Issue with $ionicUser.androidInit(token);


#1
 TypeError: $ionicPush.androidInit is not a function
        at controllers.js?ionicCachebuster=15151:80
        at Scope.parent.$get.Scope.$emit (ionic.bundle.js?ionicCachebuster=15151:24919)
        at ionic-push.js?ionicCachebuster=15151:161
        at Scope.parent.$get.Scope.$broadcast (ionic.bundle.js?ionicCachebuster=15151:24992)
        at ng-cordova.js?ionicCachebuster=15151:5684
        at ionic.bundle.js?ionicCachebuster=15151:26536
        at completeOutstandingRequest (ionic.bundle.js?ionicCachebuster=15151:14221)
        at ionic.bundle.js?ionicCachebuster=15151:14493

I know it’s a function because this is in the ionic-push.js file:

function androidInit(token) {
    // Push the token into the user data
    try {
      $ionicUser.push('_push.android_tokens', token, true);
    } catch(e) {
      console.warn('Received push token before user was identified and will not be synced with ionic.io. Make sure to call $ionicUser.identify() before calling $ionicPush.register.');
    }
  }

And I’m calling it using

$rootScope.$on('$cordovaPush:tokenReceived', function(event, data) {
    console.log('Got token', data.token, data.platform);
      $ionicPush.androidInit($scope.user);
  });

Any help would be appreciated. This is an android-only app so I don’t need to worry about iOS. If you know a different way to persist push tokens that would work too.


#2

I noticed that androidInit is already called by ionic-push

 if (ionic.Platform.isAndroid() && notification.event == "registered") {
        /**
         * Android handles push notification registration in a callback from the GCM service (whereas
         * iOS can be handled in a single call), so we need to check for a special notification type
         * here.
         */
        console.log('$ionicPush:REGISTERED', notification.regid);
        $rootScope.$emit('$cordovaPush:tokenReceived', {
          token: notification.regid,
          platform: 'android'
        });
        androidInit(notification.regid);
      }

So that tells me it should already save the token. Now, looking through the FAQ, I see it says > If you are using $ionicPush.register() without passing a user object, you should make sure you are waiting for the $ionicUser.identify() promise to complete. You can read more about this here.
But the link is dead. I assume that means I should pass a user object, but I saw somewhere that you can just add an empty .then to the register. I’ll be testing that out.


#3

It worked! All I had to do is add the following:

$ionicPlatform.ready(function (){
    $ionicPush.register({
      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) {
            alert(notification);
      }
    }).then(function(deviceToken) {
        $scope.token = deviceToken;
    });
      }

It had something to do with the promise. The $scope.token part is not needed, just the promise.