简体   繁体   English

Firebase 消息传递错误:在获取令牌之前使用服务工作者

[英]Firebase messaging error: using service worker before get token

I'm trying to use FCM with angularjs, so after initializing firebase I wrote the following code:我正在尝试将 FCM 与 angularjs 一起使用,因此在初始化 firebase 后,我编写了以下代码:

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:问题是最后一行有时会捕获出现在控制台中的错误,代码为"messaging/use-sw-before-get-token" ,消息显示:

FirebaseError: Messaging: You must call useServiceWorker() before calling getToken() to ensure your service worker is used. FirebaseError: Messaging: 您必须在调用 getToken() 之前调用 useServiceWorker() 以确保使用您的 Service Worker。 (messaging/use-sw-before-get-token). (消息/使用-sw-before-get-token)。

And as you can see in the code above, I only call getToken() after calling useServiceWorker() and after requestPermission()正如你可以在上面的代码中看到,我只叫getToken()调用后useServiceWorker()和后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我在第 35 行挖掘了原始firebase-messaging.js文件,但不幸的是没有得到任何关于为什么会发生这种情况的线索

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灵感来自阅读 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 .我发现使用firebase.initializeApp(config)比使用navigator.serviceWorker.register更好。 It can be called multiple times without having to worry about whether there is a service worker loaded.可以多次调用它,而不必担心是否加载了 Service Worker。 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.com/docs/reference/js/firebase.messaging.Messaging#gettoken尝试使用新版本的 firebase https://firebase.google.com/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);
                    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM