简体   繁体   English

如何将自定义代码添加到 Angular cli 生成的 Service Worker

[英]How to add custom code to Angular cli generated service worker

I can generate and config the service worker (by config file) generated by angular cli without issues.我可以毫无问题地生成和配置由 angular cli 生成的 service worker(通过配置文件)。 However there seems no documentation on how to add custom code the ngsw-worker.js, since i would like to add functions like push notification, post message etc. Wonder to plug in to the ngsw-worker.js然而,似乎没有关于如何在 ngsw-worker.js 中添加自定义代码的文档,因为我想添加推送通知、发布消息等功能。想插入 ngsw-worker.js

You can subscribe to the messages in your app:您可以订阅应用中的消息:

import { SwPush } from '@angular/service-worker';

this.SwPush.messages.subscribe(message => {
    console.log('[App] Push message received', message)
}

Check out this article: https://medium.com/google-developer-experts/a-new-angular-service-worker-creating-automatic-progressive-web-apps-part-2-practice-3221471269a1查看这篇文章: https : //medium.com/google-developer-experts/a-new-angular-service-worker-creating-automatic-progressive-web-apps-part-2-practice-3221471269a1

You just need to create your own service worker file.您只需要创建自己的 Service Worker 文件。

First, lets start by creating our own very basic service worker .首先,让我们从创建我们自己的非常基本的 service worker 开始

//my-service-worker.js

self.addEventListener('notificationclick', (event) => {
  console.log('notification clicked!')
});

Awesome, now in your App module change the service worker registration太棒了,现在在您的 App 模块中更改 Service Worker 注册

//app.module.ts

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

You will also need to add your service worker to your assets in your angular.json file您还需要将您的服务工作者添加到您的 angular.json 文件中的资产中

//angular.json

  "assets": {
  ...,
  "src/my-service-worker.js"
}

Great!太棒了! Now you are all set, but we just lost all of the stuff that the angular service worker provides!现在,你都设置,但我们只是失去了所有的角服务人员提供的东西! Hang on, we're not finished yet.等等,我们还没有完成。 Go back to your custom service worker and change it up返回您的自定义服务人员并进行更改

//my-service-worker.js

importScripts('./ngsw-worker.js');
self.addEventListener('notificationclick', (event) => {
  console.log('notification clicked!')
});

All Right!好吧! Now we have everything working, the angular cli accepting push notifications, and we can add our own fluff to the notification events!现在我们一切正常,angular cli 接受推送通知,我们可以将我们自己的绒毛添加到通知事件中!

https://medium.com/@smarth55/extending-the-angular-cli-service-worker-44bfc205894c https://medium.com/@smarth55/extending-the-angular-cli-service-worker-44bfc205894c

Starting with Angular8 I've found a way to use the worker-plugin to compile custom ServiceWorker code:Angular8开始,我找到了一种使用worker-plugin来编译自定义 ServiceWorker 代码的方法:

Prerequisites:先决条件:

  • Angular8角8
  • Enable Service Workers in Angular ( "serviceWorker": true , etc)在 Angular 中启用 Service Workers ( "serviceWorker": true等)
  • Enable WebWorkers in Angular ( "webWorkerTsConfig": "src/tsconfig.workers.json" )在 Angular 中启用 WebWorkers ( "webWorkerTsConfig": "src/tsconfig.workers.json" )

See: Angular: Using TypeScript for Service Worker (ServiceWorkerModule)请参阅: Angular:为 Service Worker 使用 TypeScript (ServiceWorkerModule)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM