Firebase:更改服务工作者的位置 [英] Firebase: change the location of the service worker

查看:124
本文介绍了Firebase:更改服务工作者的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Firebase消息传递(Web)。 Firebase默认搜索保存服务工作者的文件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.

服务工作者脚本应该位于应用程序的绝对路径上!例如: 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消息传递?

So, How to change the location of the service worker for firebase messaging?

推荐答案

正如Michael所说,使用的方法是 useServiceWorker(< registration>)

As Michael has called out, the method to use is useServiceWorker(<registration>).

https://firebase.google.com/docs/reference/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:更改服务工作者的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆