我正在尝试为我的 vue.js 项目实现 firebase,但我在 firebase.messaging.getToken()
上遇到错误。这是我的问题的截图
错误:
该脚本具有不受支持的 MIME 类型('text/html')。
无法加载资源:net::ERR_INSECURE_RESPONSE
无法获得通知权限。
浏览器错误消息 : “无法注册 ServiceWorker:脚本具有不受支持的 MIME 类型(‘text/html’)。”
代码 : “消息传递/失败的 serviceworker 注册”
留言 : “消息传递:我们无法注册默认服务 worker 。无法注册 ServiceWorker:脚本具有不受支持的 MIME 类型('text/html')。(消息传递/服务 worker 注册失败)。”
堆栈 : “FirebaseError:消息传递:我们无法注册默认服务 worker 。无法注册 ServiceWorker:脚本具有不受支持的 MIME 类型('text/html')。(消息传递/服务 worker 注册失败)。↵ 在评估( webpack-internal:///./node_modules/@firebase/messaging/dist/index.esm.js:1950:32)”
这是我的代码。
firebase.messaging().requestPermission().then(function () {
console.log('Notification permission granted.')
return firebase.messaging().getToken()
.then(function (currentToken) {
console.log(currentToken)
})
}).catch(function (err) {
console.log('Unable to get permission to notify.', err)
})
服务 worker
有谁知道如何解决这个问题?我尝试了很多方法,但我无法弄清楚。你能帮帮我吗?
谢谢。
最佳答案
尝试将文件(或创建文件)“firebase-messaging-sw.js”放入/public 文件夹
在 firebase-messaging-sw.js 中复制并粘贴此内容(并更改 YOUR-SENDER-ID):
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
'messagingSenderId': 'YOUR-SENDER-ID'
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
为我工作(使用 React)
关于javascript - 为什么在 Vue js 中尝试从 firebase 检索 token 时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50703652/