Pushpad
Articles › Pushpad, Sending Notifications, Web Push Notifications

Schedule or delay a web notification (like a reminder)

  • # push-api
  • # web-notifications

How can you create a web notification now that is displayed some time later?

Let's say that you are building a website and you want to allow users to be reminded of something (e.g. a deadline, an appointment, a reminder, an alarm after a few minutes or hours, etc).

Naive solution

One of the easiest solutions would be to use the JavaScript setTimeout() to display the notification some time later (most browsers support values up to 25 days).

However there is a major problem: nobody keeps a page open for a long time. When the user closes the browser window the timeout and the notification are lost. So this JavaScript function is not very useful.

Push API / Web Push with cron job on the server

The only solution to delay a notification is to use the Push API / Web Push (because it can deliver the notifications even when the website is closed and the browser is closed):

  1. When you decide to schedule a notification, tell your server about it
  2. After some time, your server sends the notification to the user (through a push service)
  3. The Service Worker is activated by the push message and displays the notification.

For example, you can create a table in your database with all the scheduled notifications and run a cron job every minute (or hour, or day) that sends the scheduled notifications (i.e. it sends all the reminders that are scheduled to be sent at that specific time). Each row in the database should also keep track of the recipient of the notification. A tuple in the "reminders" table of the database could be [send_at, user_id, message].

Tip: the cron job should select all the past messages that are not marked as sent. For example you can use send_at <= NOW() AND NOT sent. Otherwise, if you select the messages only for the current minute and the cron job is skipped for some minutes you will lose that reminders.

Using Pushpad for the scheduled notifications

If you are using Pushpad for sending web push notifications it's easy to send the notification (reminder) to a specific user. This is extremely useful if you use the cron job strategy described above for sending the reminders. The cron job strategy is quite simple to implement and allows to send notifications / reminders at any time in the future.

However, if you use Pushpad, you have also another alternative: you can create scheduled notifications directly in Pushpad (max 5 days in the future). Note that the Push API standard (alone) doesn't allow to create scheduled notifications: this feature is offered only by some advanced push services like Pushpad.

Here's an example of scheduled notification (it's written in Ruby, but there are also many other supported languages and a REST API):

notification = Pushpad::Notification.new({
  body: 'Reminder: something',
  send_at: 2.days.from_now
});
notification.deliver_to user_id

You can also read this related example.