在React JS应用程序中生成Firebase令牌问题

发布于 2025-02-11 11:47:10 字数 1399 浏览 1 评论 0原文

importScripts("https://www.gstatic.com/firebasejs/9.8.3/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/9.8.3/firebase-messaging-compat.js");




// Initialize the Firebase app in the service worker by passing the generated config
const firebaseConfig = {
  apiKey: "AIzaSyDk4dYeydr4q3npWx4iqKWejq33r1E",
  authDomain: "tengage-33e12.fireeapp.com",
  projectId: "tengage-33e12",
  storageBucket: "tengage-33e12.appspot.com",
  messagingSenderId: "1076165210",
  appId: "1:1076165210:web:c9560924940a068ce3",
  measurementId: "G-5FMCR8M0G8",
};

firebase.initializeApp(firebaseConfig);

// Retrieve firebase messaging
const messaging = firebase.messaging();

messaging.onBackgroundMessage(function (payload) {
  console.log(" message ", payload);

  const notificationTitle = payload.notification.title;
  const notificationOptions = {
    body: payload.notification.body,
  };

  self.registration.showNotification(notificationTitle, notificationOptions);
});

我有一个React JS项目,必须在其中集成FCM。

我在公共文件夹中添加了firebase-messaging-sw.js。

并添加了Firebbase依赖性。

当我试图获得令牌时,我会遇到错误。

SecurityError:未能使用脚本('http:// localhost:3005/firebase-messaging-sw.js')注册Scope('http:// localhost:3005/')的服务工作人员: ('text/html')。

但是,当我试图在新创建的应用程序中获得令牌时,借助同样的firebase凭据。

importScripts("https://www.gstatic.com/firebasejs/9.8.3/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/9.8.3/firebase-messaging-compat.js");




// Initialize the Firebase app in the service worker by passing the generated config
const firebaseConfig = {
  apiKey: "AIzaSyDk4dYeydr4q3npWx4iqKWejq33r1E",
  authDomain: "tengage-33e12.fireeapp.com",
  projectId: "tengage-33e12",
  storageBucket: "tengage-33e12.appspot.com",
  messagingSenderId: "1076165210",
  appId: "1:1076165210:web:c9560924940a068ce3",
  measurementId: "G-5FMCR8M0G8",
};

firebase.initializeApp(firebaseConfig);

// Retrieve firebase messaging
const messaging = firebase.messaging();

messaging.onBackgroundMessage(function (payload) {
  console.log(" message ", payload);

  const notificationTitle = payload.notification.title;
  const notificationOptions = {
    body: payload.notification.body,
  };

  self.registration.showNotification(notificationTitle, notificationOptions);
});

I have one React js project in which I have to integrate the FCM.

I have added firebase-messaging-sw.js in public folder.

and added the firebbase dependency also.

when I am trying to get token i am getting the error.

SecurityError: Failed to register a ServiceWorker for scope ('http://localhost:3005/') with script ('http://localhost:3005/firebase-messaging-sw.js'): The script has an unsupported MIME type ('text/html').

But with the same firebase credentials when I tried to get the token in the newly created application its giving the token.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2025-02-18 11:47:10

提到的错误可能是由于一些原因而发生的,其中之一可能是设计模式或语法中的错误。但是,这意味着最初对未知文本文件的任何请求都会被重定向到index.html,因此即使它实际上是JavaScript或SVG或其他其他类型的纯文本文件,也要使用MIME类型的MIME类型返回。
例如,请参阅下面:

// Load React App 
    // Serve HTML file for production
      if (env.name === "production")  {
            app.get("*", function response(req, res) {
                res.sendFile(path.join(__dirname, "public""index.html"));
          }); 
  }

您可以尝试在通配符路由之前添加service-worker.js的特定路由:

    app.get("/service-worker.js", (req, res) => {
         res.sendFile(path.resolve(__dirname, "public", "service-worker.js")); 
      }); 
    app.get("*", function response(req, res) { 
         res.sendFile(path.join(__dirname, "public", "index.html")); 
     });

现在,当浏览器寻找servication-worker.js时,它将使用它返回正确的哑剧类型。
(请注意,如果您尝试在通配符之后添加services-worker.js路线,则它将无法使用,因为通配符路线将覆盖。)

您还可能需要检查url

您也可以参考下面的链接以获取更多见解:
[1]: https://firebase.google.com/docs/云 - 密码/js/client#access_the_registration_token
[2]: https://firebase.google.com/docs/docs/docs/docs/cloud-messaging/服务器
[3]: FCM GetToken()未能注册服务工作人员的范围错误flutter web

The error that is mentioned may occur due to a few reasons , one of which could be an error in the design pattern or syntax. However, it means that any requests for unknown text files initially get redirected to index.html, and therefore return with the MIME type of "text/html", even if it's actually a JavaScript or SVG or some other kind of plaintext file.
Refer below for example:

// Load React App 
    // Serve HTML file for production
      if (env.name === "production")  {
            app.get("*", function response(req, res) {
                res.sendFile(path.join(__dirname, "public""index.html"));
          }); 
  }

You may try to add a specific route for service-worker.js before the wildcard route:

    app.get("/service-worker.js", (req, res) => {
         res.sendFile(path.resolve(__dirname, "public", "service-worker.js")); 
      }); 
    app.get("*", function response(req, res) { 
         res.sendFile(path.join(__dirname, "public", "index.html")); 
     });

Now, when the browser looks for service-worker.js, it will return it with the correct MIME type.
(Note that if you try adding the service-worker.js route after the wildcard, it won't work because the wildcard route will override.)

You also may want to check if the URL http://localhost:3005/firebase-messaging-sw.js, is getting served and its MIME type from the browser interface.
Also , please make sure that the service worker file is generated (Chrome DevTools -> Sources -> Filesystem). You may receive such an error if the above example service-worker.js file is not generated at all (check your deployment script).

You may also refer the links below for more insight:
[1] : https://firebase.google.com/docs/cloud-messaging/js/client#access_the_registration_token
[2] : https://firebase.google.com/docs/cloud-messaging/server
[3] : FCM getToken() Failed to register a ServiceWorker for scope error Flutter web

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文