title: How to send Firebase cloud messaging token to server after login in Angularjs web app?
body: <p>I want to send firebase cloud messaging device token to server after the user log in but it is created on loading 'index.html' Page.</p>
  
  <p>Here is my code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>"Index.html"
  navigator.serviceWorker.register('/firebase-messaging-sw.js')
              .then(function (registration) {
  
                  var serviceWorker;
                  if (registration.installing) {
                      serviceWorker = registration.installing;
                  } else if (registration.waiting) {
                      serviceWorker = registration.waiting;
                  } else if (registration.active) {
                      serviceWorker = registration.active;
                  }
                  // if (serviceWorker) {
                  //     // console.log("ServiceWorker phase is:", serviceWorker.state);
                  //     // registration.pushManager.subscribe({userVisibleOnly: true}).then(function (subscription) {
                  //     //     isPushEnabled = true;
                  //     //
                  //     //     console.log("subscription.endpoint: ", subscription.endpoint);
                  //     //
                  //     //     // sendSubscriptionIDtoServer(subscription.endpoint);
                  //     // });
                  //
                  // }
              }).catch(function (err) {
              console.log('ServiceWorker registration failed: ', err);
          });
  
  
          messaging.requestPermission()
              .then(function() {
                  console.log('Notification permission granted.');
                  messaging.getToken()
                      .then(function(currentToken) {
                          if (currentToken) {
                              console.log(currentToken);
                              localStorageService.set('fcmtoken',currentToken);
                              // sendTokenToServer(currentToken);
                              //updateUIForPushEnabled(currentToken);
                          } else {
                              // Show permission request.
                              // console.log('No Instance ID token available. Request permission to generate one.');
                              // Show permission UI.
                              //updateUIForPushPermissionRequired();
                              //setTokenSentToServer(false);
                          }
                      })
                      .catch(function(err) {
                          // console.log('An error occurred while retrieving token. ', err);
                          showToken('Error retrieving Instance ID token. ', err);
                          setTokenSentToServer(false);
                      });
              })
              .catch(function(err) {
                  // console.log('Unable to get permission to notify.', err);
              });
          messaging.onMessage(function(payload) {
  
              console.log("Message received. ", payload);
              // ...
          });</code></pre>
  </div>
  </div>
  </p>
  
  <p>In firebase-messaging-sw.js file ,</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>importScripts('https://www.gstatic.com/firebasejs/3.5.0/firebase-app.js');
  importScripts('https://www.gstatic.com/firebasejs/3.5.0/firebase-messaging.js');
  
  // /*
  // Initialize the Firebase app in the service worker by passing in the messagingSenderId.
  // */
  firebase.initializeApp({
      'messagingSenderId': '969941201331'
  });
  
  /*
  Retrieve an instance of Firebase Messaging so that it can handle background messages.
  */
  var message = firebase.messaging();
  
  message.setBackgroundMessageHandler(function(payload) {
      console.log('[firebase-messaging-sw.js] Received background message ', payload);
      // Customize notification here
      const notificationTitle = 'Background Message Title';
      const notificationOptions = {
          body: 'Background Message body.',
          icon: '/firebase-logo.png'
      };
  
      return self.registration.showNotification(notificationTitle,
          notificationOptions);
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>But now I am not able to access these function in any other controller of my app. How can I <strong>send device token along with user id</strong> to server after user Log in?</p>
  
  <p>How to access these function in any controller? Here <strong>Index.html</strong> is landing page of <strong>website(login page).</strong></p>