Firebase服务工作进程已安装但未激活

eoxn13cs  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(126)

从Concole中,我看到我的ServiceWorker已经安装,但我想我需要激活它才能从Firebase接收消息。

我是这样称呼它的:
navigator.serviceWorker.register(“/firebase-messaging-sw.js”);
我使用curl发送消息,它似乎是工作:

我做了进一步调查。服务工作线程似乎没有按预期执行。由于这一点,我得到一个错误时,使用两个初始important根据下面的代码,我删除了他们,然后工人注册,但没有启动/执行。我得到这个错误:

Uncaught DOMException: 
Failed to execute 'importScripts' on 'WorkerGlobalScope': 
The script at 'https://www.gstatic.com/firebasejs/10.4.0/firebase-app-compat.js' failed to load.
at https://"MYSERVER".org:/firebase-messaging-sw.js:4:1

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

通过把文件放在我自己的服务器上,我能够继续,但仍然没有消息。
我注意到Firebase service worker仍处于“installed”状态,但我有一个“old”处于活动状态。我怎么能确定我只有一个?

g6ll5ycj

g6ll5ycj1#

这个代码为我工作。初始化人

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

//Firebase Push  Notifications
const firebaseConfig = {
    apiKey: "AIzaSyXXXXXXXXXXXXXXXXXXXXX",
    authDomain: "bsharp-XXX-app1.firebaseapp.com",
    projectId: "bsharp-XXX_123123123",
    storageBucket: "bsharp-XXX-123123123.appspot.com",
    messagingSenderId: "123123123123",
    appId: "12312312312312312312312312",
    measurementId: "XXXXXXX"
};

// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();

navigator.serviceWorker.register("/firebase-messaging-sw.js").then(swReg2 => {
    console.log("Firebase Worker is registered", swReg2);
})

// Call this when for example the user pushes a button. 
// Need to be a user action to get it running. Especially on iOS.
function request_acceptance_for_getting_push_notificaitons(){
    Notification
        .requestPermission()
        .then(() => {
             message("Notifications allowed");
             return messaging.getToken();
    })
    .then(token => {
        console.log( "Token Is : " + token) ; // Store in DB?
    })
    .catch(err => {
        console.log("No permission to send push", err);
    });
  
}
  
messaging.onMessage(payload => {
    console.log("Message received. ");
    const { title, body, ...options } = payload.notification;
    console.log(title);
    console.log(body);
});

这是firebase-messaging-sw.js代码

importScripts("/firebase-app-compat.js")
importScripts("/firebase-messaging-compat.js")


firebase.initializeApp({
    apiKey: "AIzaSyXXXXXXXXXXXXXXXXXXXXX",
    authDomain: "bsharp-XXX-app1.firebaseapp.com",
    projectId: "bsharp-XXX_123123123",
    storageBucket: "bsharp-XXX-123123123.appspot.com",
    messagingSenderId: "123123123123",
    appId: "12312312312312312312312312",
    measurementId: "XXXXXXX"
});

const messaging = firebase.messaging();

messaging.onBackgroundMessage((payload) => {
    console.log(
    '[firebase-messaging-sw.js] Received background message ',
    payload
    );
});

相关问题