I have been working on implementing reminder notifications for my progressive web app, which was originally built as a Nextjs site. The specific criteria I need to meet are:
- Functionality even when the device is offline
- Accurate notifications within seconds
- Ability to work when the app is in the background
- Support for delays ranging from several minutes to several hours
Initially, I focused on using a service worker with a handler for the message
event. I used event.waitUntil
to prolong the life of the service worker for notifications by incorporating a delay from Bluebird's Promise.delay
.
This method worked well until the service worker was terminated around the 5-minute mark after the app was sent to the background, as outlined in the documentation. Many others have encountered this limitation as well, so I have explored various workarounds.
The accepted answer to this question lists several development options from 2019:
Periodic Background Sync API
This API allows scheduling periodic events while the app has network connectivity. However, it relies on a site's engagement score to determine the rate of syncs, lacks offline support, and is not supported by Firefox and Safari browsers, making it unsuitable for my needs.
Notification Triggers
Notifications can be triggered to show up at specific times, but the issue tracking this feature has been unresolved since 2018.
Scheduled Task API
This API permits running tasks without requiring connectivity, but it has been deprioritized.
Additional options beyond using a service worker are detailed in this resource.
Dedicated Worker Relay
This method involves holding calls to the service worker in a dedicated worker and passing them along when the notifications are due. However, this solution is not functional for mobile due to lifespan limitations.
Shared Worker Relay
Similar to Dedicated Worker Relay, but utilizing a Shared Worker, which is not supported on mobile.
Other potential workarounds include:
- Developing a native wrapper or variant that must be distributed through an app store, eliminating the PWA aspect and requiring a store deployment
- Implementing push notifications, although this approach necessitates the service managing client timers and sacrifices offline functionality
I am still seeking ways to enable offline notifications. I have explored whether keeping a notification open can sustain the service worker's lifespan (as native apps do) and if there are any special permissions that can extend service worker longevity, but I have not made progress on these fronts yet.
Has anyone achieved success in achieving this kind of functionality?