Android PushNotifications with PushPlugin on emulator

I've been following this sample to get android pushNotifications (with GCM) working on an android emulator. After $cordovaPush.register(config) I get Ok as response. But it never runs my callback [$scope.$on('$cordovaPush:notificationReceived']. And in consequence I never get my registration ID.

I've created a google API project. And I'm using that project Id in the config.senderID when calling $cordovaPush.register(config).

I've also registered a gmail account into my emulator.

I guess I have 2 questions.

1.Is it possible to get (and register) push notifications on an android emulator?

  1. why don't I get a $cordovaPush:notificationReceived event that calls my callback?

    app.controller('AppCtrl', function($scope, $cordovaPush, $cordovaDialogs, $cordovaMedia, $cordovaToast, ionPlatform, $http) { $scope.notifications = [];

    // call to register automatically upon device ready
    ionPlatform.ready.then(function (device) {
        $scope.register();
    });
    
    
    // Register
    $scope.register = function () {
        var config = null;
    
        if (ionic.Platform.isAndroid()) {
            config = {
                "senderID": "12834957xxxx" 
            };
        }
    
        $cordovaPush.register(config).then(function (result) {
            console.log("Register success " + result);
    
            $cordovaToast.showShortCenter('Registered for push notifications');
            $scope.registerDisabled=true;
    
        }, function (err) {
            console.log("Register error " + err)
        });
    }
    
    
    $scope.$on('$cordovaPush:notificationReceived', function (event, notification) {
        console.log(JSON.stringify([notification]));
        if (ionic.Platform.isAndroid()) {
            handleAndroid(notification);
        }
    
    });
    
    // Android Notification Received Handler
    function handleAndroid(notification) {
        // ** NOTE: ** You could add code for when app is in foreground or not, or coming from coldstart here too
        //             via the console fields as shown.
        console.log("In foreground " + notification.foreground  + " Coldstart " + notification.coldstart);
        if (notification.event == "registered") {
            $scope.regId = notification.regid;
            storeDeviceToken("android");
        }
        else if (notification.event == "message") {
            $cordovaDialogs.alert(notification.message, "Push Notification Received");
            $scope.$apply(function () {
                $scope.notifications.push(JSON.stringify(notification.message));
            })
        }
        else if (notification.event == "error")
            $cordovaDialogs.alert(notification.msg, "Push notification error event");
        else $cordovaDialogs.alert(notification.event, "Push notification handler - Unprocessed Event");
    }
    

The fix is a lot simpler than it seems. I changed from:

$scope.$on('$cordovaPush:notificationReceived', function (event, notification) {

to:

$scope.$on('pushNotificationReceived', function (event, notification) {

while debugging I noticed this on ng-cordova.js:

angular.module('ngCordova.plugins.push', [])
    .factory('$cordovaPush', ['$q', '$window', '$rootScope', function ($q, $window, $rootScope) {
        return {
            onNotification: function (notification) {
                $rootScope.$apply(function () {
                    $rootScope.$broadcast('pushNotificationReceived', notification);
                });
            },

That means that it's doing a broadcast for 'pushNotificationReceived'. Not 'notificationReceived' as documented.

I have the same problem. It has been reported in ngCordova github, but no replies yet.

I managed to fix it. I know it's not a good solution if you are using angular but it's the only way I'm able to catch the register Id.

  1. Inside the config object you must specify the 'ecb':

    var androidConfig = {
        "senderID": "388573974286",
        "ecb": "function_to_be_called"
    };
    
  2. Put outside the controller the function:



        window.function_to_be_called = function (notification) {
        switch(notification.event) {

            case 'registered':
                if (notification.regid.length > 0 ) {
                    alert('registration ID = ' + notification.regid);
                }
                break;

            case 'message':
                // this is the actual push notification. its format depends on the data model from the push server
                alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
                break;

            case 'error':
                alert('GCM error = ' + notification.msg);
                break;

            default:
                alert('An unknown GCM event has occurred');
                break;
        }
    };

Even if you update the event name as you said, I also have another problem, which says: processmessage failed: message: jjavascript:angular.element(document.queryselector('[ng-app]')).injector().get('$cordovapush').onnotification({"event":"registered"...

It happens when angular doesn't find 'ng-app' and it returns 'undefined' angular.element(document.querySelector('[ng-app]'))

So to solve this, you can set manually your 'ecb' like this:

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

Thanks to this ionic forum entry

please update your ng-cordova.js in lib/ng-cordova.js from http://ngcordova.com/docs/install/ in ng-cordova-master\dist\ng-cordova.js