【问题标题】:Angular 6 - "Service workers are disabled or not supported" errorAngular 6 - “服务工作者被禁用或不受支持”错误
【发布时间】:2019-03-01 23:31:24
【问题描述】:

我想使用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);
}

返回错误:

错误:此浏览器已禁用或不支持 Service Worker 在 SwPush.push../node_modules/@angular/service-worker/fesm5/service-worker.js.SwPush.requestSubscription (service-worker.js:146) 在新的 DashboardComponent (dashboard.component.ts:40) 在 createClass (core.js:9311) 在 createDirectiveInstance (core.js:9186) 在 createViewNodes (core.js:10406) 在 createRootView (core.js:10320) 在 callWithDebugContext (core.js:11351) 在 Object.debugCreateRootView [as createRootView] (core.js:10838) 在 ComponentFactory_.push../node_modules/@angular/core/fesm5/core.js.ComponentFactory_.create (core.js:8666) 在 ComponentFactoryBoundToModule.push../node_modules/@angular/core/fesm5/core.js.ComponentFactoryBoundToModule.create (core.js:3315)

预期结果:

【问题讨论】:

  • 可能你的浏览器不支持网络推送?看这个问题stackoverflow.com/questions/48045748/…
  • 我在 chrome 上运行它。但我发现这是因为我使用ng serve 运行应用程序,而服务人员无法使用ng serve。我使用http-server 再次运行该应用程序,它运行良好。
  • 好吧,没错

标签: angular push-notification


【解决方案1】:

此错误是由于服务人员不使用ng serve 造成的。使用http-server 运行,然后它工作正常。 如Angular service workers所说:

因为 ng serve 不适用于服务工作者,所以您必须使用单独的 HTTP 服务器在本地测试您的项目。您可以使用任何 HTTP 服务器

【讨论】:

  • .. 本地主机除外
  • 是否可以使用 ng serve 禁用 localhost 上的错误?
  • 我用 http-server 运行它,但仍然得到错误
【解决方案2】:

只需在“AppModule”中设置服务工作者在开发环境中工作

ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production})

ServiceWorkerModule.register('ngsw-worker.js', { enabled: true})

【讨论】:

猜你喜欢
  • 2020-09-13
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
相关资源
最近更新 更多