Push Notification: Registration Returns OK but Never Received Registration ID ($cordovaPush) [SOLVED]

Hi,

I have followed the sample on ngCordova page http://ngcordova.com/docs/plugins/pushNotifications/ for Android.

Instead of “document.addEventListener”, I used the code on “$ionicPlatform.ready”. So far it seems to be working.

Code:

.run(function($ionicPlatform, $cordovaPush) {
    
  $ionicPlatform.ready(function() {
    
    //push notification register  
    var configAndroid = {"senderID" : "1111111111111"};
    
    $cordovaPush.register(configAndroid)
    .then(function(result) {
        //alert("result = " + result);
    },
    function(err){
        //alert(err);
    })
    
    
    //handle notifications
    $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
        switch(notification.event) {
            
            case 'registered': //register confirmation
                if (notification.regid.length > 0 ) {
                    alert('registration ID = ' + notification.regid);
                }
                break;
                
            case 'message': //actual push notification    
                alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
                break;
            
            case 'error': //Error
                alert('GCM error = ' + notification.msg);
                break;
                
            default:
                alert('An unknown GCM event has occurred');
                break;
        }
        
    })
    
  
})

When tested on my device, I am getting “OK” on register function but somehow never got registration ID.

On this thread Android PushNotifications with PushPlugin on emulator it suggest to use “notificationReceived” instead of “$cordovaPush:notificationReceived”. I have tried that also but without any success.

This thread Push notification using PushPlugin returns OK and not reg id suggest to modify the “config.xml” but I am not so sure about that. I am expecting that “ng” version of plugins should have taken care of those things.

Also I am using Project Number (not project ID) as this thread suggests http://stackoverflow.com/questions/28202565/can-t-get-push-notifications-in-android-to-work-using-ngcordova but no success

Need some suggestion.

Thanks!

Hi,

After a brief tweaking, I found the solution. So far the code is fine. Just needed to include the “$rootScope” on the “run” function. Not obvious at first.

Hope this might help newbies stumbling to this situation. Documentation is not up-to-date.

So the code should look like this:

.run(function($ionicPlatform, $rootScope, $cordovaPush) {
    
  $ionicPlatform.ready(function() {
    
    //push notification register  
    var configAndroid = {"senderID" : "1111111111111"};
    
    $cordovaPush.register(configAndroid)
    .then(function(result) {
        //alert("result = " + result);
    },
    function(err){
        //alert(err);
    })
    
    
    //handle notifications
    $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
        switch(notification.event) {
            
            case 'registered': //register confirmation
                if (notification.regid.length > 0 ) {
                    alert('registration ID = ' + notification.regid);
                }
                break;
                
            case 'message': //actual push notification    
                alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
                break;
            
            case 'error': //Error
                alert('GCM error = ' + notification.msg);
                break;
                
            default:
                alert('An unknown GCM event has occurred');
                break;
        }
        
    })
    
  
})

http://ngcordova.com/docs/plugins/pushNotifications/ needs to update the documentation for this plugin.

Thanks!