javascript - Firebase messaging error: using service worker before get token - Stack Overflow

I'm trying to use FCM with angularjs, so after initializing firebase I wrote the following code:me

I'm trying to use FCM with angularjs, so after initializing firebase I wrote the following 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)});

the problem is that the last line sometimes catches an error which appears in console with code "messaging/use-sw-before-get-token" and message that says:

FirebaseError: Messaging: You must call useServiceWorker() before calling getToken() to ensure your service worker is used. (messaging/use-sw-before-get-token).

And as you can see in the code above, I only call getToken() after calling useServiceWorker() and after requestPermission()

I dug into the original firebase-messaging.js file in line 35 but unfortunately didn't get any clue of why this is happening

I'm trying to use FCM with angularjs, so after initializing firebase I wrote the following 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)});

the problem is that the last line sometimes catches an error which appears in console with code "messaging/use-sw-before-get-token" and message that says:

FirebaseError: Messaging: You must call useServiceWorker() before calling getToken() to ensure your service worker is used. (messaging/use-sw-before-get-token).

And as you can see in the code above, I only call getToken() after calling useServiceWorker() and after requestPermission()

I dug into the original firebase-messaging.js file in line 35 but unfortunately didn't get any clue of why this is happening

Share Improve this question asked Mar 7, 2018 at 10:40 Hassan AjkHassan Ajk 1352 silver badges7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

I know this is probably crazy and will backfire but it works.

 .
 .
 .
 if('undefined' !== typeof messaging.b )
      delete(messaging.b);
 messaging.useServiceWorker(registration);
 .
 .
 .

inspired by reading firebase-messaging.js

I had a similar crazy idea to your answer:

if (!firebase.apps[0]) {
    firebase.initializeApp(config);
}

I found that it's better to use firebase.initializeApp(config) than navigator.serviceWorker.register. It can be called multiple times without having to worry about whether there is a service worker loaded. Just put this on every page in your app:

var config = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "..."
}

firebase.initializeApp(config);

Try to user new version firebase https://firebase.google./docs/reference/js/firebase.messaging.Messaging#gettoken

 messaging.getToken({
                        serviceWorkerRegistration: registration,
                        vapidKey: keyPair
                    }).then((currentToken) => {
                        if (currentToken) {
                            console.log(currentToken)
                        } else {
                            // Show permission request UI
                            console.log('No registration token available. Request permission to generate one.');
                        }
                    }).catch((err) => {
                        console.log('An error occurred while retrieving token. ', err);
                    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744298096a4567361.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信