简体   繁体   English

Firebase:更改服务工作者的位置

[英]Firebase: change the location of the service worker

I am trying to use Firebase messaging (web).我正在尝试使用 Firebase 消息传递(网络)。 Firebase by default searches for the file "firebase-messaging-sw.js" which holds the service worker.默认情况下,Firebase 搜索包含 Service Worker 的文件“firebase-messaging-sw.js”。

The service worker script is expected to be on the absolute path of the application! service worker 脚本应该在应用程序的绝对路径上! For example : http://localhost/firebase-messaging-sw.js例如: http://localhost/firebase-messaging-sw.js

How to change this default location?!如何更改此默认位置?! Searching the official docs I found this method: useServiceWorker which accepts a service worker registeration, but trying to use it I get an error that the method doesn't even exist!搜索官方文档我找到了这个方法: useServiceWorker接受服务工作者注册,但尝试使用它我得到一个错误,该方法甚至不存在!

So, How to change the location of the service worker for firebase messaging?那么,如何更改 service worker 的位置以进行 firebase 消息传递?

As Michael has called out, the method to use is useServiceWorker(<registration>) .正如迈克尔所说,要使用的方法是useServiceWorker(<registration>)

https://firebase.google.com/docs/reference/js/firebase.messaging.Messaging#useServiceWorker https://firebase.google.com/docs/reference/js/firebase.messaging.Messaging#useServiceWorker

The Messaging class is what is returned when you call firebase.message() . Messaging 类是您调用firebase.message()时返回的内容。

So the sample would be:所以样本是:

navigator.serviceWorker.register('./example/sw.js')
.then((registration) => {
  messaging.useServiceWorker(registration);

  // Request permission and get token.....
});

I just tried this on the demo app on Github without issue:https://github.com/firebase/quickstart-js/tree/master/messaging我刚刚在 Github 上的演示应用程序上尝试了这个没有问题:https ://github.com/firebase/quickstart-js/tree/master/messaging

You should be able to do firebase.messaging().useServiceWorker(registration) and pass in an existing service worker registration.您应该能够执行firebase.messaging().useServiceWorker(registration)并传入现有的服务工作者注册。 Note that you should do this as early as possible, before any calls to eg getToken() or onMessage() .请注意,在对getToken()onMessage()任何调用之前,您应该尽早执行此操作。

I know that it is an old question but as useServiceWorker is deprecated now, it is good to mention that you can pass the service worker directly to the getToken .我知道这是一个老问题,但由于useServiceWorker现在已被弃用,很高兴提到您可以将 service worker 直接传递给getToken Current document当前文档

const registration = await navigator.serviceWorker.ready;

const messaging = firebase.messaging();

const token = await messaging.getToken({
            serviceWorkerRegistration: registration,
            vapidKey: "",
        });

By using useServiceWorker(registration) you can use notificationonclick event on Firebase push messaging via exiting service worker.通过使用useServiceWorker(registration)您可以通过退出 Service Worker 在 Firebase 推送消息上使用notificationonclick事件。

firebase.messaging().useServiceWorker(registration) is working and solved my problem, but keep in mind that I don't know if: firebase.messaging().useServiceWorker(registration)正在工作并解决了我的问题,但请记住,我不知道:

  • this is a secure way to get payload from the server.这是从服务器获取有效负载的安全方式

  • this is the best way to handle push notifications.这是处理推送通知的最佳方式。

I think it is:我觉得是这样的:

const vapidKey = 'foo123...';
const serviceWorkerRegistration = await navigator
    .serviceWorker
    .register('/build/firebase-messaging-service-worker.js');

await getToken(messaging, {
    vapidKey,
    serviceWorkerRegistration,
})...

But note that if you copy this code the scope gets wrong I think (because of the path, in my example: /build/... ).但请注意,如果您复制此代码,我认为范围会出错(因为路径,在我的示例中: /build/... )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM