检索令牌时发生错误。 FirebaseError:消息传递:我们无法注册默认服务工作者

问题描述 投票:0回答:1

我正在尝试将 FCM 集成到我的反应(带有打字稿)应用程序中。我正在尝试获取 FCM 令牌,但我不断收到此错误:

检索令牌时发生错误。 FirebaseError:消息传递:我们无法注册默认服务工作线程。无法使用脚本('http://localhost:3000/firebase-messaging-sw.js')注册范围('http://localhost:3000/firebase-cloud-messaging-push-scope')的ServiceWorker:该脚本具有不受支持的 MIME 类型(“text/html”)。 (消息传递/服务工作人员注册失败)。

这是我的代码:

import { initializeApp } from "firebase/app";
import { getMessaging,getToken   } from "firebase/messaging";

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};

export const app = initializeApp(firebaseConfig);

const fcmMessaging = getMessaging();
getToken(fcmMessaging, { vapidKey: 'xcv' }).then((currentToken) => {
  if (currentToken) {
    console.log(currentToken,"FCM token")
  } else {

    console.log('No registration token available. Request permission to generate one.');
  }
}).catch((err:any) => {
  console.log('An error occurred while retrieving token. ', err);
});
reactjs firebase google-cloud-platform firebase-cloud-messaging messaging
1个回答
0
投票

只需替换此 import { getMessaging} from 'firebase/messaging'; 使用此 import { getMessaging} from 'firebase/messaging/sw';

© www.soinside.com 2019 - 2024. All rights reserved.