gpt4 book ai didi

javascript - 未定义 Firebase 消息传递 importScripts

转载 作者:行者123 更新时间:2023-11-30 19:16:16 27 4
gpt4 key购买 nike

我在Firebase平台上写了一个WEB应用

我有一个文件 “firebase-messaging-sw.js”,当我从 home.html 页面访问该文件时,出现错误:

importScripts is not

文件 “firebase-messaging-sw.js” 位于根级别。

主页.html:

<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="shortcut icon" href="img/favicon.png"/>
<meta name="keywords" content="" />

<link rel="stylesheet" href="css/reset.css" type="text/css" />
<link rel="stylesheet" href="css/home.css" type="text/css" media="screen and (min-width:901px)" />
<link rel="stylesheet" href="css/mhome.css" type="text/css" media="screen and (max-width:900px)" />

</head>

<body>
<header>
...
</header>

<div class="function">
...
</div>

<footer>
...
</footer>

<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-storage.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-messaging.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-performance.js"></script>

<script src="javaScript/initFirebase.js"></script>
<script src="javaScript/homeFirebase.js"></script>
<script src="javaScript/initHomePage.js"></script> 
<script src="javaScript/latlon-geohash.js"></script>

<script src="firebase-messaging-sw.js"></script>

</body>

firebase-messaging-sw.js:

importScripts('https://www.gstatic.com/firebasejs/6.3.3/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/6.3.3/firebase-messaging.js');

firebase.initializeApp({
'messagingSenderId': '123456654321'
});

帮助修复错误!谢谢!

最佳答案

firebase-messaging-sw.js是服务 worker JavaScript 文件。

所以你不应该使用 <script src="firebase-messaging-sw.js"></script> 加载 JavaScript 文件.

删除 <script src="firebase-messaging-sw.js"></script>并尝试以下代码。

    <script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/firebase-messaging-sw.js');
});
}
</script>

参见 https://developers.google.com/web/fundamentals/primers/service-workers/#register_a_service_worker

关于javascript - 未定义 Firebase 消息传递 importScripts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57962568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com