Firebase 消息传递 importScripts 未定义

Firebase messaging importScripts is not defined(Firebase 消息传递 importScripts 未定义)
本文介绍了Firebase 消息传递 importScripts 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I write a WEB application on the platform Firebase

我有一个文件 "firebase-messaging-sw.js",当我从 home.html 页面访问该文件时,我得到一个错误:

I have a file "firebase-messaging-sw.js", and when I access the file from home.html page, I get an error:

importScripts 不是

importScripts is not

文件 firebase-messaging-sw.js" 位于根级别.

home.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:

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 是 service worker JavaScript 文件.

firebase-messaging-sw.js is service worker JavaScript file.

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

So you should not load JavaScript file using <script src="firebase-messaging-sw.js"></script>.

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

Remove <script src="firebase-messaging-sw.js"></script> and try the following code.

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

请参阅 https://developers.google.com/web/基础/入门/服务工作者/#register_a_service_worker

这篇关于Firebase 消息传递 importScripts 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)