我想向我的 Angular 应用发送推送通知。我是用 vanilla js 做的,而不是用 Angular service worker 或 @angular/pwa。我在angular.json的脚本选项中添加了js文件的路径。当要注册 Service Worker 时,它会从根目录 ( http://127.0.0.1:8887/sw.js )重定向路径,而不是从我放置文件的文件夹中重定向。这是 angular.json 文件: "scripts": [ "./src/assets/js/main.js", "./src/assets/js/sw.js" ]main.js 文件:const applicationServerPublicKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';let isSubscribed = false;let swRegistration = null;if ('serviceWorker' in navigator && 'PushManager' in window) { console.log('Service Worker and Push is supported'); navigator.serviceWorker.register('/sw.js') // I tried ./sw.js and also /src/assets/js/sw.js and sw.js alone .then(function(swReg) { console.log('Service Worker is registered', swReg); swRegistration = swReg; }) .catch(function(error) { console.error('Service Worker Error', error); }); } else { console.warn('Push messaging is not supported'); pushButton.textContent = 'Push Not Supported'; }服务工作者文件仅用于测试该文件目前是否可访问:console.log('sw');这是错误:Service Worker Error TypeError: Failed to register a ServiceWorker for scope ('http://127.0.0.1:8887/') with script ('http://127.0.0.1:8887/sw.js'): A bad HTTP response code (404) was received when fetching the script.
1 回答
皈依舞
TA贡献1851条经验 获得超3个赞
问题是决心,我不得不添加的路径/assets/js/sw.js
到navigator.serviceWorker.register('/assets/js/sw.js')
添加回答
举报
0/150
提交
取消