Reputation: 21
I'm implementing push notification service. I want send a variable e.regid to AppCtrl controller in AngularJS. Could any one help me solving this problem..?
PushCustom.js
function onNotification(e) {
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
//alert("regID = " + e.regid);
AppCtrl(e.regid);
}
break;
case 'message':
if (e.foreground) {
var soundfile = e.soundname || e.payload.sound;
var my_media = new Media("/android_asset/www/" + soundfile);
my_media.play();
}
break;
case 'error':
break;
default:
break;
}
}
app.js
app.controller('AppCtrl',
['$scope','$http', function($scope, $http, e.regid){
alert(e.regid);
$scope.regid = e.regid;
}]);
Anyone got any idea on how to make this work? Thanks!
Upvotes: 0
Views: 374
Reputation: 1314
Use AngularJS Service, as share variable between controllers. Make setter and getter service for it.
Upvotes: 1
Reputation: 259
switch (e.event) { case 'registered': if (e.regid.length > 0) { //******************* new line ********************// angular.element(document).scope().$broadcast('GCM_PUSHER',e.regid) } break; case 'message': if (e.foreground) { var soundfile = e.soundname || e.payload.sound; var my_media = new Media("/android_asset/www/" + soundfile); my_media.play(); } break; case 'error': break; default: break; } }
Then listen broadcast event in your controller:
app.controller('AppCtrl',['$scope','$http', function($scope, $http){ $scope.$on('GCM_PUSHER',function($event,body){ //********* logic here guys *********// }); }]);
Upvotes: 0
Reputation: 1549
You can use Broadcast or emit depending on your scope. And catch it in your controller. Go through : https://docs.angularjs.org/api/ng/type/$rootScope.Scope - broadcast, emit and $on.
Upvotes: 0