问题描述
我正在尝试使用 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:更改服务人员的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)