ngCordova $cordovaPush and Push Notifications


#1

Hey guys, im trying to get the new ngCordova Push notifications to work.

I installed the plugin cordova plugin add https://github.com/phonegap-build/PushPlugin.git and added the example code but i keep getting Unknown provider: $cordovaPushProvider <- $cordovaPush.

What am i doing wrong?

Thanks in advance.


#2

Are you including ng-cordova.js before or after cordova.js? This is a common issue I’ve seen.

http://ngcordova.com/docs/


#3

Thank you very much, that was my problem. But now im getting TypeError: Cannot read property 'pushNotification' of undefined when i try to use the example code in my controller.


#4

try to run $cordovaPush after platform is ready

controller('MyCtrl', function($scope, $ionicPlatform, $cordovaPush) {
  $ionicPlatform.ready(function() {
  $cordovaPush.register({...});
  });
});

that works for me :blush:


#5

What parameters we need to pass in options while calling $cordovaPush unregister

 $cordovaPush.unregister(options).then(function (result) {
            // Success! 
        }, function (err) {
          // An error occured. Show a message to the user
        });

#6

how to handle incoming message


#7

I has try but got ReferenceError: cordova is not defined error


#8

I had to wrap $cordovaPush calls in $ionicPlatform.ready() too.

I assume all plugins would be affected?

Shouldn’t ngCordova handle this or at least document it?


#9

My interpretation of the ngCordova docs was that the callbacks are non-Angular functions. A common Use Case when processing a notification is to change the state/view of the app - ie. $state.go(). To access $state you’ll need to manually inject it outside of Angular (unless somebody can pipe in with a cleaner solution).

function onNotificationGCM (event) {
    
    angular.element(document).injector().invoke(function ($state) {
        $state.go('a_specific_chat_window');
      });
}

#10

True, maybe ngCordova should document it.


#11

But you have to try it on device, it won’t work in browser.


#12

HI,

Is’t work for Win8 and WinPhone?


#13

Hi,
I’m having the same problem arielgpe had in the first place. I keep getting this error Unknown provider: pushProvider <- push. I’ve tried including ng-cordova.js before and after cordova.js and it’s still failing.

Here’s my code:

angular.module('diaadia.pushService', [])
.factory('PushProcessingService', ['$http', function($http) {
        function onDeviceReady() {
            var pushNotification = window.plugins.pushNotification;
            pushNotification.register(gcmSuccessHandler, gcmErrorHandler, {
                "senderID": "mySenderID",
                "ecb": "onNotificationGCM"
            });
        }
        function gcmSuccessHandler(result) {
            console.info('NOTIFY  pushNotification.register succeeded.  Result = '+result)
        }
        function gcmErrorHandler(error) {
            console.error('NOTIFY  '+error);
        }
        return {
            initialize : function () {
                document.addEventListener('deviceready', onDeviceReady, false);
            },
            registerID : function (id) {
                var feedURL = 'myURL';
                var successCallback = function(data, status, headers, config) {
                    console.log(data);
                };
                var errorCallback = function(data, status, headers, config, statusText){
                    console.log(data);
                }
                return $http.post(feedURL, {'notification_id': id}).success(successCallback).error(errorCallback);
            }, 
        }
    }]);
 
    function onNotificationGCM(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    //console.log('REGISTERED with GCM Server -&gt; REGID:' + e.regid + "");
                    var elem = angular.element(document.querySelector('[ng-app]'));
                    var injector = elem.injector();
                    var pushService = injector.get('PushProcessingService');
                    pushService.registerID(e.regid);
                }
                break;
            case 'message':
                break;
            case 'error':
                break;
            default:
                break;
        }
    }

I’m able to register the ID in the database so the code apparently it’s working but I don’t understand why I’m having this error…any idea?

Thanks!


#14

Hi, Ionic
I think that we need more docs on ngCordova Push plugin. Could you create a tutorial?


#15

I had this problem, in the end I solved it by adding ‘ngCordova’ to the ‘starter’ module dependency

    angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ionic.utils', 'ngCordova'])

  .run(function ($ionicPlatform, $localstorage, $cordovaPush) {
    $ionicPlatform.ready(function () {

...

#16

calling $cordovaPush.register in a controller doesnt call back ecb, any luck anyone?


#17

Same issue here. After calling regiser(), I don’t get a call back with the registration id.


#18

are you running on a actual device or in simulator or browser

my experience is it needs to be on the actual device to work


#19

Actual device. I’m on a Nexus 5. I actually posted an issue about this with sample code, but I can’t find the post. Not sure if the team removed it for whatever reason.

I have it posted on stackoverflow as well:


#20

you should be able to put ‘alerts’ in the ngcordova source and see if the events are being posted