如何解决JavaScript通知和Service Worker API:为什么注册后serviceWorker.ready可以承诺?
我试图使用JavaScript为我的Web应用程序创建一个简单的通知功能。为此,我尝试注册一个服务人员,该服务人员将发送通知以支持action
之类的通知API。
这是我的代码:
1 navigator.serviceWorker.register('/scripts/dummy.js');
2
3 function notify(title,data) {
4 console.log('function call');
5 if (Notification.permission == 'granted' && defaultStatus != 'dnd') {
6 console.log('permission and not dnd');
7 navigator.serviceWorker.ready.then(function(registration) {
8 console.log('ready,send notification);
9 registration.showNotification(title,data);
10 });
11 }
12 }
问题出在navigator.serviceWorker.ready
Chrome开发者控制台显示this。我需要在dummy.js
中放些东西吗? (目前为空,但我认为以前可以使用)
解决方法
我解决了我的问题。之所以不起作用,是因为我将dummy.js
从当前目录移到了子目录。
这是将选项{ scope: '/' }
添加到serviceWorker.register
函数后,我从Chrome收到的错误:
The path of the provided scope ('/') is not under the max scope allowed ('/scripts/user/'). Adjust the scope,move the Service Worker script,or use the Service-Worker-Allowed HTTP header to allow the scope.
(index):1 Uncaught (in promise) DOMException: Failed to register a ServiceWorker for scope ('https://mydomain/') with script ('https://mydomain/scripts/user/user_worker.js'): The path of the provided scope ('/') is not under the max scope allowed ('/scripts/user/'). Adjust the scope,or use the Service-Worker-Allowed HTTP header to allow the scope.
有关更多信息,请参见this的答案
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。