问题描述
我正在尝试使用 Firebase 消息传递(网络).Firebase 默认搜索包含 service worker 的文件firebase-messaging-sw.js".
I am trying to use Firebase messaging (web). Firebase by default searches for the file "firebase-messaging-sw.js" which holds the service worker.
Service Worker 脚本应位于应用程序的绝对路径上!例如:http://localhost/firebase-messaging-sw.js
The service worker script is expected to be on the absolute path of the application! For example : http://localhost/firebase-messaging-sw.js
如何更改此默认位置?!搜索官方文档我发现了这个方法:useServiceWorker
,它接受服务工作者注册,但尝试使用它我得到一个错误,该方法甚至不存在!
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!
那么,如何更改用于 Firebase 消息传递的 Service Worker 的位置?
So, How to change the location of the service worker for firebase messaging?
推荐答案
正如 Michael 所说,使用的方法是 useServiceWorker(
.
As Michael has called out, the method to use is useServiceWorker(<registration>)
.
https://firebase.google.com/docs/参考/js/firebase.messaging.Messaging#useServiceWorker
Messaging 类是您调用firebase.message()
时返回的内容.
The Messaging class is what is returned when you callfirebase.message()
.
所以样本是:
navigator.serviceWorker.register('./example/sw.js')
.then((registration) => {
messaging.useServiceWorker(registration);
// Request permission and get token.....
});
我刚刚在 Github 上的演示应用上尝试了这个,没有问题:https://github.com/firebase/quickstart-js/tree/master/messaging
I just tried this on the demo app on Github without issue: https://github.com/firebase/quickstart-js/tree/master/messaging
这篇关于Firebase:更改服务人员的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!