服务工作者已注册,但navigator.serviceWorker.ready
显示为空。
navigator.serviceWorker.register('sw.js', {scope: './'}).then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
//Check subscription state
checkSubscription();
});
})
function checkSubscription() {
console.log('fun checkSubscription');
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration){
serviceWorkerRegistration.pushManager.getSubscription().then(
function(pushSubscription) {
if(!!pushSubscription) {
console.log(pushSubscription);
pushStatus = true;
}
else {
pushStatus = false;
}
}.bind(this)).catch(function(e) {
console.error('Error getting subscription', e);
});
});
}
以上是我的代码。这在Firefox浏览器中运行良好。
答案 0 :(得分:0)
如果在Google Chrome上运行服务工作者,则需要在服务器上运行您的网站,如apache(xampp)/ nodejs(http-server)。