I developed a web application that functions as a messaging system, where users can submit messages and others can receive them. The system operates through AJAX, with the front end using JavaScript to interact with a PHP backend. Everything runs smoothly without any issues.
In addition, I integrated a Notification system that sends push notifications to desktop or Android apps when a new message is received. This feature also works flawlessly.
However, the problem arises with the notification system when the Android device is out of focus. The setTimeout function used to periodically check the PHP AJAX system becomes unreliable - sometimes working, sometimes not at all, and sometimes delivering notifications very late.
To address this issue, I attempted to transfer the functionality to a service worker, assuming it would operate independently of the browser focus. Unfortunately, this solution proved to be even worse, with the service worker being even less consistent than using setTimeout in the browser.
Is there a way to resolve this issue? Are there specific directives within the service worker that can be implemented to prevent it from sleeping?
Thank you.