Best practice to get FCM token on app first run

When I install my app it installs as expected. So buttons are displayed to open the app or close the installation box. If I click to open the app I cannot get FCM token (registration id) on this first app run.

I have to close it and open it again clicking on its icon and everything works fine (I get the token while splashscreen is running and the app moves to the register user page where I get “token” from localStorage).

I made some tests to get the token from localstorage and it looks like localstorage is not returning it to my var token = localStorage.getItem(“token”) on signup page loading in a first moment.

I created a button to get it and it´s working by the way. I don´t know what´s happening with localstorage.

Any suggestion to make it works on app first run inside $ionicPlatform.ready() ?


.run(function($ionicPlatform, $state, $ionicPopup) {
 $ionicPlatform.ready(function() {

     var push = PushNotification.init({
            android: {
                senderID: "************",
                sound: true,
                vibrate: true
            },
            browser: {},
            ios: {
                alert: "true",
                badge: "true",
                sound: "true"
            },
            windows: {}
        });

        push.on('registration', function(data) {
            //data.registrationId;
            localStorage.setItem("token", data.registrationId);
            console.log("token: "+data.registrationId);
        }); 

        push.on('notification', function(data) {
            // data.message,
            // data.title,
            // data.count,
            // data.sound,
            // data.image,
            // data.additionalData         
            
        });

        push.on('error', function(e) {
            // e.message
            //alert("Error: " +e.message);
        });

     navigator.splashscreen.hide();
...