title: Firebase messaging error: using service worker before get token
body: <p>I'm trying to use FCM with angularjs, so after initializing firebase I wrote the following code:</p>
  
  <pre><code>messaging = firebase.messaging();
  $window.navigator.serviceWorker.register($rootScope.app.base_url + '/app/lib/firebaseCustomWorker.js')
  .then(function(registration) {
      messaging.useServiceWorker(registration);
      messaging.requestPermission().then(function(){  
      messaging.getToken()
      .then(function(currentToken) {
          console.log(currentToken);
      })
      .catch(function(err) {
          console.log('An error occurred while retrieving token. ', err);
      });
  }).catch(function(err){console.log(err)});
  </code></pre>
  
  <p>the problem is that the last line sometimes catches an error which appears in console with code <code>"messaging/use-sw-before-get-token"</code> and message that says:</p>
  
  <blockquote>
    <p>FirebaseError: Messaging: You must call useServiceWorker() before calling getToken() to ensure your service worker is used. (messaging/use-sw-before-get-token).</p>
  </blockquote>
  
  <p>And as you can see in the code above, I only call <code>getToken()</code> after calling <code>useServiceWorker()</code> and after <code>requestPermission()</code></p>
  
  <p>I dug into the original <code>firebase-messaging.js</code> file in line 35 but unfortunately didn't get any clue of why this is happening</p>