我有firebase-messaging-sw.js文件,该文件在网络中可见,并且也在控制台中记录了消息。但是我现在越来越:
An error occurred while retrieving token. FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. (messaging/failed-serviceworker-registration).
at https://www.gstatic.com/firebasejs/6.6.1/firebase-messaging.js:1:29909:
我试图检查控制台并进行调试,但不确定是什么错误。
firebase-messaging-sw.js文件包含:
importScripts('https://www.gstatic.com/firebasejs/4.1.3/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/4.1.3/firebase-messaging.js');
console.log("in sw page");
index.html具有:
firebase.initializeApp({
apiKey: "XXXXXX",
authDomain: "XXXX-XXXXX.firebaseapp.com",
databaseURL: "XXXXXXX",
projectId: "XXXXXX",
storageBucket: "",
messagingSenderId: "XXXXXXX",
appId: "XXXXX"
});
// // Initialize Firebase
const messaging = firebase.messaging();
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
navigator.serviceWorker.register('firebase-messaging-sw.js')
console.log('Notification permission granted.');
messaging.getToken().then((currentToken) => {
if (currentToken) {
console.log(currentToken);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
});
} else {
console.log('Unable to get permission to notify.');
}
});
我希望用户单击“允许”后会获得令牌。 可能是什么原因?还有解决方案吗?