You cannot receive push notifications in Android to work with ngCordova

It is very difficult for me to receive push notifications (using the ngCordova plugin) to work. I executed their sample code exactly as described 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 application starts, I get an โ€œOKโ€ warning, so I know that it successfully passes through a call to $ cordovaPush.register. However, I expected to receive a โ€œregisteredโ€ notice right after, but I never get a notification.

Any help would be appreciated.

+8
javascript angularjs cordova ionic
source share
2 answers

The solution is in the comments, but for this you need the correct answer.

First of all, the register callback always returns OK while you pass the senderID , but if the $cordovaPush:notificationReceived event is never raised (this may take a few seconds), this identifier is probably incorrect.

You should use the Project Number , not the project ID.

To get the number, go to the API Console , select the project, and you will be taken to the Overview page. At the top of this page you will see something like this:

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

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

+8
source share

I suffer a lot from this, and I found that there are actually two versions of the pushto cordova plugin:

Both are supported by ngCordova, but only an outdated version is documented.

The legacy version is $ cordovaPush and the newer is $ cordovaPushV5 , and they have completely different methods.

For me, the problem was that I downloaded the cord plugin-push and tried to implement it with the old documentation on the ngCordova website.

The code:

  /* * Non deprecated version of Push notification events */ function registerV5() { $ionicLoading.show({ template: '<ion-spinner></ion-spinner>' }); if (ionic.Platform.is('browser')) { alert("You are running on broswer, please switch to your device. Otherwise you won't get notifications"); $ionicLoading.hide(); return; } /** * Configuration doc: * https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#pushnotificationinitoptions */ var GCM_PROJECT_ID = 'xxxxxx'; $cordovaPushV5.initialize({ "android": { "clearNotifications": false, "senderID" : GCM_PROJECT_ID } }); $cordovaPushV5.register().then(function (deviceToken) { console.log("Successfully registered", deviceToken); $scope.data.deviceToken = deviceToken; // Below code required to configure $cordovaPushV5 notifications emitter. // Don't pass function it not handler. $cordovaPushV5.onNotification(); $cordovaPushV5.onError(); $ionicLoading.hide(); }, function (error) { console.log("Failed to registered"); console.log("error object : ", error); $ionicLoading.hide(); }); } $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data) { console.log("notification received"); console.log("data object: ", data); var foreground = data.additionalData.foreground || false; var threadID = data.additionalData.payload.threadID || ''; var group = data.additionalData.payload.group || false; if (foreground) { // Do something if the app is in foreground while receiving to push - handle in app push handling console.log('Receive notification in foreground'); } else { // Handle push messages while app is in background or not started console.log('Receive notification in background'); // Open FB messanger app when user clicks notification UI when app is in background. if (typeof data.additionalData.coldstart != "undefined" && data.additionalData.coldstart == false) if (!group) // Open FB Messenger of specific user chat window window.open('fb-messenger://user/' + threadID, '_system', 'location=no'); else // Open FB Messenger of specific group chat window window.open('fb-messenger://groupthreadfbid/' + threadID, '_system', 'location=no'); } }); $rootScope.$on('$cordovaPushV5:errorOccurred', function(event, error) { console.log("notification error occured"); console.log("event object: ", event); console.log("error object: ", error); }); 

More about this github article: https://github.com/driftyco/ng-cordova/issues/1125 (code from here) and in this article: <a3>

+1
source share

All Articles