0

I'm trying to return this .register() method:

    var deferred = $q.defer();
    $cordovaPushV5.initialize($constants.PUSH_OPTIONS).then(function () {
        $cordovaPushV5.onError();
        $cordovaPushV5.onNotification();

        return $cordovaPushV5.register();
    }).then(function (push_token) {
        deferred.resolve(push_token);
    });
    return deferred.promise;

But it is never resolving with push_token... This seems to be correct according to other questions i've seen, However I can't get it to work properly.. Any help is greatly appreciated. Thanks!.

Aravind
  • 40,391
  • 16
  • 91
  • 110
user1027620
  • 2,745
  • 5
  • 37
  • 65
  • You mean that `then( fun (push_token))` triggers before `register` is done? Also share `initialize`. – AndreaM16 Nov 26 '16 at 15:06
  • Guys sorry i think it's my bad, seems $cordova push notifications will trigger only if running on a mobile device and won't work in the simulator. I will double check this and get back to you. Thanks for the help tho. – user1027620 Nov 26 '16 at 15:26
  • 1
    Don't use `$q.defer()` to compose promises. You already have a promise from `$cordovaPushV5.initialize`, wrapping it with `defer` will just lead to mistakes (in this case you've omitted to propagate the error callback to the deferred). – teppic Nov 27 '16 at 03:28

1 Answers1

1

I don't know about Cordova, but Promise seems fine.
But it can be simplified :

    return $cordovaPushV5.initialize($constants.PUSH_OPTIONS)
        .then(function () {
           $cordovaPushV5.onError();
           $cordovaPushV5.onNotification();
           return $cordovaPushV5.register();
        }, function(error) {
            console.log(error);
    });
gr3g
  • 2,866
  • 5
  • 28
  • 52
  • Check update : If you add the error function, you might uderstand what's going wring – gr3g Nov 26 '16 at 15:24