Articles › Pushpad, Sending Notifications, Web Push Notifications

Unique notifications in a period of time (prevent duplicated notifications)

Sometimes an event can be triggered very frequently. However you want to send a notification to the user only once in a period of time: for example you may want to notify the event with at most one notification every hour (or day, etc.).

We have already discussed how to throttle notifications in a previous post. Here we just give a more practical example about the use of a Redis lock to prevent duplicated notifications in a period of time. We'll use Ruby, but the example can be easily adapted to any language.

# call this method every time the event is triggered
# and this method will send at most one notification per day
def send_notification_once
  acquire_lock, _ = $redis.with do |conn|
    conn.multi do |multi|
      multi.setnx 'notification_lock', ''
      multi.expire 'notification_lock', 1.day.to_i
  return unless acquire_lock
  # now you can actually send the notification...