Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can’t get push notifications in Android to work using ngCordova

I'm having a tough time getting push notifications (using the ngCordova plugin) to work. I have followed their sample code exactly as is documented on the site: http://ngcordova.com/docs/plugins/pushNotifications/

(the only difference is that I don't have a deviceready listener, instead, my code is inside the ionicPlatform.ready listener.)

Here is my code:

angular.module('myApp', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $rootScope, $state, $cordovaPush) {
  $ionicPlatform.ready(function() {
    var config = {
      "senderID": "myID100001000"
    };

    $cordovaPush.register(config).then(function(result) {
      alert(result);
    }, function(err) {
      alert(err);
    })      
  }); 

  $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
    switch(notification.event) {
      case 'registered':
        if (notification.regid.length > 0 ) {
          alert('registration ID = ' + notification.regid);
        }
        break;

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

When my app starts I do get the "OK" alert, so I know it successfully goes through the $cordovaPush.register call. However, I was expecting to get a "registered" notification event, right after, but I never get notified.

Any help would be appreciated.

like image 249
Prabhu Avatar asked Jan 28 '15 21:01

Prabhu


1 Answers

The solution is in the comments but this needs a proper answer.

First of all, the register callback always returns OK as long as you pass a senderID, but if the $cordovaPush:notificationReceived event is never called (it may take a few seconds), this ID is probably wrong.

You must use the Project Number, not the Project ID.

To get the number, go to the API Console, select the project and you'll be on the Overview page. On top of this page, you'll see something like this:

Project ID: your-project-id        Project Number: 0123456789

Just copy and use the project number and everything should work.

like image 102
Marc Climent Avatar answered Oct 28 '22 23:10

Marc Climent