如果我在requestPermission之后立即调用Firebase的getToken,我会在控制台中收到一个错误,为什么?

And*_* SK 3 javascript push-notification firebase firebase-cloud-messaging

如果我在requestPermission之后立即调用Firebase的getToken,则大多数情况下会在控制台中收到错误消息。这是我当前的代码:

<script src="https://www.gstatic.com/firebasejs/4.12.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.12.1/firebase-messaging.js"></script>
<script>
var config = {
apiKey: "XXXXXXXXX",
authDomain: "XXXXXXXXX.firebaseapp.com",
databaseURL: "https://XXXXXXXXX.firebaseio.com",
projectId: "XXXXXXXXX",
storageBucket: "XXXXXXXXX.appspot.com",
messagingSenderId: "XXXXXXXXX"
};
firebase.initializeApp(config);
</script>
<script>
function notification_permission() {
    messaging.requestPermission().then(function(permission) {
        console.log('Notification permission granted', permission);
        messaging.getToken().then(function(token) {
            if(token) {
                //update user token
                console.log('Token acquired', token);
            } else {
                //you don't have permission to show notifications
            }
        }).catch(function(err) {
            //retrieving token failed, analyze the error
            console.error('Retrieving token failed', err);
        });
    }).catch(function(err) {
        console.error('Unable to get permission to notify', err)
    });
}
</script>
Run Code Online (Sandbox Code Playgroud)

如果浏览器已经具有通知权限,并且我致电notification_permission();,那么我会在控制台中收到令牌而没有任何问题。但是,如果这是用户第一次按“ 允许”授予站点权限以接收通知,则将触发此错误(并非总是,但在大多数情况下):

POST https://fcm.googleapis.com/fcm/connect/subscribe 404 ()

Retrieving token failed e {code: "messaging/token-subscribe-failed", message: "Requested entity was not found.", stack: "FirebaseError: Requested entity was not found."}
Promise.catch (async)
Promise.then (async)
(anonymous) @ VM1243:1
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?谢谢!

Ada*_*ane 5

试用最新版本(4.13.0)。我在发行说明中看到了针对此问题的修复程序,现在看来对我来说还可以。
https://firebase.google.com/support/release-notes/js