为什么在 Vue js 中尝试从 firebase 检索令牌时出现错误?

Reo*_*o93 7 javascript firebase vue.js vuejs2

我正在尝试为我的 vue.js 项目实施 firebase,但我在firebase.messaging.getToken(). 这是我的问题的截图

在此处输入图片说明

错误:

该脚本具有不受支持的 MIME 类型 ('text/html')。

无法加载资源:net::ERR_INSECURE_RESPONSE

无法获得通知权限。

  • browserErrorMessage :“未能注册 ServiceWorker:脚本具有不受支持的 MIME 类型 ('text/html')。”

  • 代码:“消息/失败-serviceworker-registration”

  • 消息:“消息:我们无法注册默认的服务工作者。注册服务工作者失败:脚本具有不受支持的 MIME 类型('text/html')。(消息/失败的服务工作者注册)。”

  • 堆栈:“FirebaseError:消息:我们无法注册默认的服务工作者。无法注册服务工作者:脚本具有不受支持的 MIME 类型('text/html')。(消息/失败-serviceworker-registration)。?在评估 (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)
})
Run Code Online (Sandbox Code Playgroud)

服务工作者

在此处输入图片说明

有谁知道如何解决这个问题?我尝试了很多方法,但我无法弄清楚。你能帮我么?

谢谢。

Tad*_*dis 5

尝试将文件(或创建一个文件)“ 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();
Run Code Online (Sandbox Code Playgroud)

为我工作(使用 React)