[英]Firebase Messaging service worker on chrome
I am trying to implement FCM chrome client on Eclipse IDE. 我正在尝试在Eclipse IDE上实现FCM chrome客户端。
Using Javascript, I am trying to register the default service worker : firebase-messaging-sw.js but the path for this js file cannot be resolved by project. 我正在尝试使用Javascript注册默认的服务工作者:firebase-messaging-sw.js,但是此js文件的路径无法由项目解析。
The SDK is searching for this file at hosting level: /firebase-messaging-sw.js ie https://localhost:8080/firebase-messaging-sw.js but not at https://localhost:8080/myapp/firebase-messaging-sw.js SDK会在托管级别搜索此文件:/firebase-messaging-sw.js,即https:// localhost:8080 / firebase-messaging-sw.js,但不在https:// localhost:8080 / myapp / firebase-消息-sw.js
Therefore, I get Error : Firebase Service worker not found while using GWT (404 Error) 因此,我收到错误消息:使用GWT时找不到Firebase Service worker(404错误)
How can I resolve this issue ? 我该如何解决这个问题?
Service workers by default should reside on the root of the application. 默认情况下,服务工作者应驻留在应用程序的根目录下。 To register a service worker st some other location than the default location, you can use the following code.
要在默认位置以外的其他位置注册服务工作者,您可以使用以下代码。
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw-test/sw.js', {scope: '/sw-test/'})
.then(function(reg) {
// registration worked
console.log('Registration succeeded. Scope is ' + reg.scope);
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
- The outer block performs a feature detection test to make sure service workers are supported before trying to register one.
外部模块执行功能检测测试,以确保在尝试注册服务人员之前支持服务人员。
- Next, we use the ServiceWorkerContainer.register() function to register the service worker for this site, which is just a JavaScript file residing inside our app (note this is the file's URL relative to the origin, not the JS file that references it.)
接下来,我们使用ServiceWorkerContainer.register()函数为该站点注册服务工作者,这只是驻留在我们应用程序中的一个JavaScript文件(请注意,这是相对于原始文件的URL,而不是引用该URL的JS文件。 )
- The scope parameter is optional, and can be used to specify the subset of your content that you want the service worker to control.
scope参数是可选的,可用于指定您希望服务人员控制的内容子集。 In this case, we have specified '/sw-test/', which means all content under the app's origin.
在这种情况下,我们指定了“ / sw-test /”,这表示该应用程序起源下的所有内容。 If you leave it out, it will default to this value anyway, but we specified it here for illustration purposes.
如果您将其保留,则无论如何它将默认为该值,但出于说明目的,我们在此处指定了该值。
- The .then() promise function is used to chain a success case onto our promise structure.
.then()承诺函数用于将成功案例链接到我们的承诺结构。 When the promise resolves successfully, the code inside it executes.
当承诺成功解析后,其中的代码就会执行。
- Finally, we chain a .catch() function onto the end that will run if the promise is rejected.
最后,我们将.catch()函数链接到最后一个被拒绝的承诺将运行的末尾。
This registers a service worker, which runs in a worker context, and therefore has no DOM access.
这注册了一个在工作程序上下文中运行的服务工作程序,因此没有DOM访问权限。 You then run code in the service worker outside of your normal pages to control their loading.
然后,您可以在正常页面之外的服务工作者中运行代码以控制其加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.