FirebaseError:: 我们无法注册默认的 Service Worker

问题描述

FirebaseError: Messaging: 我们无法注册认的 Service Worker。无法使用脚本 ('http://127.0.0.1:8000/firebase-messaging-sw. js'):获取脚本时收到错误的 HTTP 响应代码 (404)。 (消息/失败的服务工作人员注册)。

PS:我在根文件夹中有 firebase-messaging-sw.js(即 /public )

firebase-messaging.js

/*
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/7.23.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.23.0/firebase-messaging.js');

/*
Initialize the Firebase app in the service worker by passing in the messagingSenderId.
* New configuration for app@pulseservice.com
*/
firebase.initializeApp({            
        apiKey: "XXXXXXXXXXXXXX",authDomain: "XXXXXXXXXX.firebaseapp.com",projectId: "XXXXXXXXXX",storageBucket: "XXXXXXXXXX.appspot.com",messagingSenderId: "XXXXXXXXXXXX",appId: "XXXXXXXXXXXXXXX"
        });

/*
Retrieve an instance of Firebase Messaging so that it can handle background messages.
*/
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
    console.log(
        "[firebase-messaging-sw.js] Received background message ",payload,);
    // Customize notification here
    const notificationTitle = "Background Message Title";
    const notificationoptions = {
        body: "Background Message body.",icon: "/itwonders-web-logo.png",};

    return self.registration.showNotification(
        notificationTitle,notificationoptions,);
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)