我正在我的网站上开发网络推送通知.我关注了Google的Web Push Notifications和Mozilla的The Service Worker Cookbook.
我已经在Google Chrome v50上进行了测试,一切正常,但在调用navigator.serviceWorker.register(‘./时,我会在Firefox 44,45,46,52,最新的Firefox(版本57.0.4 64位)上收到以下错误消息. push-service-worker.js’)函数.
TypeError: ServiceWorker script at 07002 for scope 07003 encountered an error during installation.
这是我的代码:
在controller.js中注册ServiceWorker
navigator.serviceWorker.register('push-service-worker.js')
.then((registration) => {
return registration.pushManager.getSubscription()
.then((subscription) => {
if (subscription) {
return subscription;
}
var subscribeOptions = {
userVisibleOnly: true,applicationServerKey: buildApplicationServerKey(),};
return registration.pushManager.subscribe(subscribeOptions);
});
})
.then((subscription) => {
sendSubscriptionToServer(subscription);
})
.catch((err) => {
console.log('Unable to subscribe to push: ',err);
});
推送服务worker.js
'use strict';
self.addEventListener('push',(event) => {
var payload = event.data.json();
var title = payload.title || 'Title';
event.waitUntil(
self.registration.showNotification(title,{
body: payload.body,icon: './common/images/notification-icon-192x192.png',image: payload.image || '',})
);
});
self.addEventListener('notificationclick',(event) => {
event.notification.close();
var urlToOpen = new URL('/',self.location.origin).href;
event.waitUntil(
clients.matchAll({
type: 'window',includeUncontrolled: true,})
.then((windowClients) => {
var matchingClient = null;
for (var i = 0; i < windowClients.length; i++) {
var windowClient = windowClients[i];
if (windowClient.url === urlToOpen) {
matchingClient = windowClient;
break;
}
}
if (matchingClient) {
return matchingClient.focus();
} else {
return clients.openWindow(urlToOpen);
}
})
);
});
目录结构
./root
---- manifest.json
---- push-service-worker.js
---- src
---- controller.js
谢谢你的帮助!
最佳答案
正如wanderview在here所说:
原文链接:https://www.f2er.com/js/429244.htmlFWIW,you should always use a separate profile for each channel (release/beta/dev-edition/nightly). We’re working on making it work like that out-of-the-Box,but its not ready yet.
当我为多个Firefox版本使用一个配置文件时遇到此问题.要解决此问题,请转到about:support并单击Refresh Firefox.如果它不起作用,您可以转到about:profiles,单击Create new profile,然后在新浏览器中启动profile.