0

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!

3 Answers 3

1

Use AngularJS Service, as share variable between controllers. Make setter and getter service for it.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0
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 *********//
    });   

}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.