如果我在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>
如果浏览器已经具有通知权限并且我调用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
知道为什么吗?谢谢!
尝试最新版本 (4.13.0(。 我在发行说明中看到了一个修复程序,现在对我来说似乎工作正常。
https://firebase.google.com/support/release-notes/js