4

I am trying to implement notifications that do not need the interaction of the backend to be shown. The use of this is to put a remind me button, that would send a notification on certain time of the day (that the user specified). I am pretty new to service workers but I understand that they work asynchronously. That being said, is there any way in which I could do something like the following pseudocode?

function timeLeft(time){
    setTimeout(() => showNotification(), time);
}

This would work if I put it on a regular javascript file and the user has the browser still open.

It does not need to be exactly like that, it just needs to solve my problem. Thank you un advance.

Tom Piaggio
  • 650
  • 8
  • 27

1 Answers1

-2
  1. First you need to have an ID for each browser. If you use the Push API, then you can use the browser endpoint.
  2. When a user adds a reminder, you must store the tuple (endpoint, send_at, reminder) in your database on your server
  3. Run a cron job on your server (or create a scheduled job with Sidekiq, etc.) in order to send the reminder at the correct time (using the Push API)

Otherwise you can use a service which allows you to create scheduled notifications: see this example on my blog.

collimarco
  • 34,231
  • 36
  • 108
  • 142
  • 4
    Hello, welcome to Stack Overflow! Please just note if you want to promote or recommend your own product/blog, there are some [guidelines in place](https://stackoverflow.com/help/promotion) for doing so. Following them will help you avoid giving the impression that you're spamming. Could you please [edit] to explicitly state your affiliation? Thanks. (If you're not actually affiliated, it may be worth mentioning that as well.) – DavidPostill May 09 '18 at 19:45
  • @DavidPostill I have updated the answer to make the affiliation explicit. – collimarco May 09 '18 at 22:30