问题描述
我试图让 Firebase FCM 在我的 React 项目中工作(使用 webpack)
Im tryin to get Firebase FCM working in my React project(using webpack )
当尝试使用 getToken() 时:
When trying to getToken() using:
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
return messaging.getToken();
})
.then(function(token) {
console.log('token')
})
.catch(function(err) {
console.log('Unable to get permission to notify.', err);
});
异常抛出如下:
browserErrorMessage: "Failed to register a ServiceWorker: The scrip
has an unsupported MIME type ('text/html')
我了解此问题与缺少的 service worker 文件有关:firebase-messaging-sw.js,我已将其添加到项目的根目录,但我仍然收到相同的错误.
I understand that this issue is related to the missing service worker file: firebase-messaging-sw.js, which I added to the root of the project but I'm still getting the same error.
我不确定我在这里做错了什么,我已经设置了 vanilla java 脚本项目,它工作正常....
Im not sure what im doing wrong here, i've set up vanilla java script project and it works fine ....
关于这个问题有什么想法吗?
Any ideas about this issue ?
推荐答案
对于那些使用create-react-app的人,你可以在public<中创建
firebase-messaging-sw.js
/code> 文件夹,并在 index.js
上添加:
For those using create-react-app, you can create the firebase-messaging-sw.js
inside public
folder and, on index.js
add:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('../firebase-messaging-sw.js')
.then(function(registration) {
console.log('Registration successful, scope is:', registration.scope);
}).catch(function(err) {
console.log('Service worker registration failed, error:', err);
});
}
这篇关于Firebase FCM React 项目问题 - firebase-messaging-sw.js 类型错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!