Angular 6 - "Service workers are disabled or not supported"错误

标签 angular push-notification

我想使用 web-push 模块和 angular service worker 推送通知订阅。我已按照以下链接中的说明进行操作:Angular Push Notifications: a Complete Step-by-Step GuidePush 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)

预期结果:

result

最佳答案

这个错误是由于 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/

相关文章:

javascript - 如何在 Angular 8 中检测文件的字符集?

angular - 获取 Firestore 的 UID 引用

Angular Material : mat-drawer-container not showing backdrop with multiple mat-drawer

node.js - Angular 2在不重新启动的情况下不显示图像

angular - <selector> 不是已知元素 Angular 9 自定义库

ios - Twilio 可编程聊天 - 推送通知未到达

iphone - 如何删除 Apple APNS 反馈收到的设备 token

ios - Azure 移动应用程序 Node.js 后端 - 推送通知错误状态代码 400

iOS 推送通知 - JavaPNS - keystore.p12 文件安全

iphone - 如何使用 Xcode 调试生产推送通知?