Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

304
Visualizações
Firebase getToken not working after changed Service Worker

For Firebase 9.5.0 push notifications, we have a service worker that receives firebase push, the service workers all work fine.

However, we're migrating from an old service worker registration (from a 3rd party) to our own, brand new users all work fine registering and getting a token, but we get an error when we want to use a new service worker for an existing registered user (with an old service-worker from a 3rd party).

The error happens on getToken which causes the error


FirebaseError: Messaging: A problem occurred while subscribing the user to FCM: 
Request is missing required authentication credential. 
Expected OAuth 2 access token, login cookie or other valid authentication credential.

I'm hoping we can add some code in the catch to do something like reregister the user or something, but looking through the firebase docs, I can't find anything how we would register the user with ourselves without an error ?

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.5.0/firebase-app.js";
import { deleteToken, getMessaging, getToken, isSupported, onMessage  } from "https://www.gstatic.com/firebasejs/9.5.0/firebase-messaging.js";


const firebaseConfig = { 
   apiKey: ...
   authDomain: ...
   projectId: ...
   storageBucket: ...
   messageSenderId: ...
   appId: ...
   measurementId: ...
};

const firebaseApp = initializeApp(firebaseConfig);
const messaging = getMessaging(firebaseApp);

const registerServiceWorker = () => {
   if ('serviceWorker' in navigator) {

     navigator.serviceWorker.register('serviceworkerfilename.js')
      .then( function( registration) {
        console.log('reg successful', registration); // gets here ok
        
        getToken( messaging, { serviceWorkerRegistration: registration } )
         .then(function(currentToken) {
           // do stuff, this normally works for a new user, but not on old service worker
         })
        .catch( function(err) {

           // always gets here for old user from old service worker
           Notification.requestPermission().then(function(permission) {
             console.log(permission); // gets here, and shows 'granted', but stuck from here what we can do to reset the user
           });

         });

      })
   }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So I got around this firstly with a check to check with filename the serviceworker had

navigator.serviceWorker.controller.scriptURL.match(<someregex>)

And then if wanted, unregister the existing service worker...

navigator.serviceWorker.getRegistrations()
  .then( function(registrations) { 
    for(let registration of registrations) { 
      registration.unregister(); 
    } 
  });

And then if necessary, call a function to register again.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda