问题
I am playing with service worker stuff. I already made it as well as manifest json. I want to trigger service worker start after following scenario:
Added push integration and sending push notification from server side. Received it in client side, but only if I am in my application page.
Here are the steps what I want to try to start service worker:
- I opened the web page for the first time
- Service - Worker is installed
- I totally close the browser
- I re-open the browser
- Now I need to trigger Service -Worker start - How ?
I want to know what I can use or what I need to add in my service worker js or manifest json to make this scenario possible.
回答1:
You cannot start the Service Worker just like that.
The Service Worker is handled by the browser and started when a matching event occurs. Web push notification is one such an event. If the user has accepted to receive notifications from your web application's backend server, then the push notification will be shown to the user through the Service Worker even if the user is not on your website.
But you cannot "just start" the Service Worker and have it do something when the browser is opened without accessing your page.
I highly recommend you to read Google's Web Fundamentals series on push notifications: https://developers.google.com/web/fundamentals/push-notifications
来源:https://stackoverflow.com/questions/58258677/how-can-i-wake-up-service-worker-when-browser-closed