How to access variables made in app.js in the services.js


#1

I’m currently writing an app that has push notification functionality. The tutorials that I’ve seen online show getting the registration ID in the app.js like so.

$cordovaPush.register(androidConfig).then(function(result) {
            //alert('Success!!')
        }, function(err) {
            alert("Android Registration Error: " + err)
        })
        
        

        $rootScope.availablePush = false;
        $rootScope.deviceID = device.uuid;
        $rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
            switch (notification.event) {
                case 'registered':
                    if (notification.regid.length > 0) {
                        
                        $rootScope.registrationID = notification.regid;
                    }
                    break;

                case 'message':
                    // this is the actual push notification. its format depends on the data model from the push server
                    $rootScope.availablePush = true;
                    break;

                case 'error':
                    $rootScope.errorMessage = notification.message;
                    alert('GCM error = ' + notification.msg);
                    break;

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

I need to pass the registration ID into a post call to a server we have which will then send out a push notification. Problem is, is that i can’t store the value in the services.js (which if I understand correctly, obviosuly don’t, is the place to keep the data and functions using the data)

angular.module('starter.services', [])
.factory('Home', function($rootScope){
var notificationStatus = $rootScope.availablePush; //true or false, true meaning there is a push, false meaning there isn't
var regID = $rootScope.registrationID;

both of those values return undefined when called from the dash through the controller.

Assuming that it’s not possible to get the registrationID in the services (though if it is, please let me know) I have no idea how to get the values that i need from app.js and store them in the services.js, so that I can send them to the server.