我想使用 web-push
模块和 angular service worker
推送通知订阅。我已按照以下链接中的说明进行操作:Angular Push Notifications: a Complete Step-by-Step Guide和 Push Notifications with Angular & Express但是通知提示没有按预期显示。我已经检查了 swPush
对象,发现它没有启用,我不知道为什么我什至完全按照 angular/pwa
安装说明进行操作链接说。希望这里有人可以提供帮助。非常感谢!
我使用自己的 Nodejs
服务器运行应用程序,而不是使用 http-server
模块。
这是我的推送订阅码:
readonly VAPID_PUBLIC_KEY =
'BIfDkegCvhzctX06rYvwQImhbfAymWYE3wtcog9E4Zj7LOgX6TQFS6fZSqLCt01zBZtc1-jSwpZrZEmTQ2i95V8'; // key generated with web-push
constructor(
private user: UserService,
private httpClient: HttpClient,
private router: Router,
private auth: AuthService,
private swPush: SwPush
) {
this.swPush.requestSubscription({
serverPublicKey: this.VAPID_PUBLIC_KEY
})
.then(subcription => {
console.log('Send ' + subcription + ' to server');
})
.catch(console.error);
}
返回错误:
Error: Service workers are disabled or not supported by this browser at SwPush.push../node_modules/@angular/service-worker/fesm5/service-worker.js.SwPush.requestSubscription (service-worker.js:146) at new DashboardComponent (dashboard.component.ts:40) at createClass (core.js:9311) at createDirectiveInstance (core.js:9186) at createViewNodes (core.js:10406) at createRootView (core.js:10320) at callWithDebugContext (core.js:11351) at Object.debugCreateRootView [as createRootView] (core.js:10838) at ComponentFactory_.push../node_modules/@angular/core/fesm5/core.js.ComponentFactory_.create (core.js:8666) at ComponentFactoryBoundToModule.push../node_modules/@angular/core/fesm5/core.js.ComponentFactoryBoundToModule.create (core.js:3315)
预期结果:
最佳答案
这个错误是由于 service workers 不使用 ng serve
造成的。使用 http-server
运行然后它工作正常。
如 Angular service workers说:
Because ng serve does not work with service workers, you must use a separate HTTP server to test your project locally. You can use any HTTP server
关于 Angular 6 - "Service workers are disabled or not supported"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52513466/