问题
I'm building a PWA that on Android uses Push Notifications to receive alerts.
Of course, Safari/iOS doesn't support Push Notifications, so I need to implement an equivalent. Can I have my Service Worker poll for data, either using http long timeouts or socket.io? On receiving an alert, the SW will focus/create an app window to then deal with the UX.
If there is a list of cans & can'ts or dos * don'ts for service workers, that would be great.
回答1:
No, that is not possible. You cannot keep the SW alive in the background at all nor have an open connection of any sort to your server. This is by design so that some SW code is not constantly running, using resources, and potentially tracking the user in some non-obvious manner.
This is the same on Android side: the SW is not running in the background constantly but instead asleep and woken by events - such as receiving a new push msg - passed to it by the OS/browser. Then after doing whatever work the event causes to happen the SW goes to "sleep" again.
来源:https://stackoverflow.com/questions/61550581/can-i-http-poll-or-use-socket-io-from-a-service-worker-on-safari-ios