在以下代码实验室中: https://developers.google.com/web/fundamentals/getting-started/codelabs/push-notifications/
我下载了push-notifications.git
项目,并将最终的/completed/08-unsubscribe
示例上传到我的HTTPS网络服务器。
当我第一次访问此页面时,控制台日志显示为:
Service Worker is supported
sw.js:24 Started
sw.js:28 Installed
sw.js:32 Activated
第二次访问该页面时读取:
Service Worker is supported
我尝试调试的问题是我没有从此函数获得Service Worker is ready
输出:
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('/sw.js').then(function() {
return navigator.serviceWorker.ready;
}).then(function(serviceWorkerRegistration) {
reg = serviceWorkerRegistration;
subscribeButton.disabled = false;
console.log('Service Worker is ready :^)', reg);
}).catch(function(error) {
console.log('Service Worker Error :^(', error);
});
}
阻止我的“订阅”按钮启用并触发console.log('Service Worker is ready :^)', reg);
或console.log('Service Worker Error :^(', error);
消息的挂断在哪里?
答案 0 :(得分:0)
您的代码是正确的。 如果你给控制台,它应该打印。我验证了它,它的确有效。 只需通过提供控制台进行检查。
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('/sw.js').then(function() {
return navigator.serviceWorker.ready;
}).then(function(serviceWorkerRegistration) {
console.log('Service Worker is ready :^)', reg);
}).catch(function(error) {
console.log('Service Worker Error :^(', error);
});
}