How to handle the Notification event in ngCordova push plugin

as a code template doing this as follows:

.controller('PushNotificationsCtrl', function ($scope, $cordovaPush) { var androidConfig = { "senderID":"372433177444", "ecb":"onNotification" }; $cordovaPush.register(androidConfig).then(function(result) { // Success! $scope.pushSuccess = result }, function(err) { $scope.pushSuccess = err; }); 

I am able to successfully get RegID from GCM. But how do I manage onNotification from androidConfig ?

+5
source share
2 answers

I found a solution.

instead of this:

 var androidConfig = { "senderID":"372433177444", "ecb":"onNotification" }; 

I like the following:

 var androidConfig = { "senderID":"372433177444", "ecb":"window.onNotification" }; 

then

 window.onNotification = function(e) { switch( e.event ) { case 'registered': if ( e.regid.length > 0 ) { console.log("Your regID is : " + e.regid); } break; case 'message': // this is the actual push notification. its format depends on the data model from the push server console.log('message = '+e.message); angular.element(document.querySelector('#yata')).html(e.message); break; case 'error': console.log('GCM error = '+e.msg); break; default: console.log('An unknown GCM event has occurred'); break; } }; 

everything works as expected now :)

+5
source

Change the following code to ng-cordova.js :

 //config.ecb = 'angular.element(' + injector + ').injector().get(\'$cordovaPush\').onNotification'; config.ecb = "angular.element(" + injector + ").injector().get('$cordovaPush').onNotification"; 

and then set the parameter configuration object as follows:

 var options = { android: { senderID: "789175757493", ecb:"onNotification", forceShow: true }, } 

It will display notifications in the notification box when you open the application.

0
source

Source: https://habr.com/ru/post/1203760/


All Articles