Pushpad
Articles › Pushpad, Targeting Users, Web Push Notifications

Sending web push notifications to specific users based on condition

  • # push-api
  • # targeting

In this article we see a frequent use case for notifications, where you want to send a notification only to the users in your database that match a specific condition.

For example, let's say that we have a numeric column in the application database and that those numbers are updated dynamically. Then sometimes we want to find the users that have a value grater than 100 and send them a notification.

Another example can be looking for users that are currently in a specific location, based on the information in our database and send them a notification.

Note that these are just some examples, but you can really filter your database based on any condition (even complex conditions).

Find the user IDs in your database based on condition

The only requirement is that your database query returns a list of user IDs that must be reached with the notification. For example:

SELECT id FROM users WHERE custom_field > 100;

Then, how can we reach those users?

Associate each web push subscription to a user ID

The answer is simple: when you create a web push subscription you need to store it together with the current user ID.

In this way you can later find the web push subscription for a specific user or a specific group of users.

For example, if you use Pushpad, you can use this Javascript code in order to subscribe the current user to notifications and store his subscription on Pushpad together with his user ID:

// create or update the web push subscription 
pushpad('subscribe', function () {}, { 
  uid: '33', // keep track of the current user ID
  uidSignature: '4b9503324d1c97ecfc551dbb377452c85da8ebb9'
});

As you can see we also add a dynamic signature (something similar to JWT) to the request in order to have authentication and prevent other users from subscribing with arbitrary user IDs.

Send the notifications to specific users

Now you have a list of user IDs (the recipients) and each web push subscription is associated to a specific user ID: you can just filter the web push subscriptions based on your list of user IDs and send the notifications.

If you use the Pushpad libraries or the Pushpad REST API that is extremely simple. Let's see an example with Ruby on Rails:

user_ids = User.where('custom_field > 100').ids

notification = Pushpad::Notification.new({
  body: "Your value is grater than 100!"
})

# deliver to specific users
notification.deliver_to user_ids

Alternative: associate some tags directly to the web push subscriptions

In the previous sections we have associated a user ID to the web push subscriptions and then filtered them with the user IDs.

Sometimes it can be useful to attach some arbitrary tags to the subscriptions and then filter based on that tags. For example you can attach the country information to the web push subscriptions in order to target only the users in a specific location.

This is similar to the steps above, but instead of a user ID we associate a tag to the subscription:

pushpad('subscribe', function () {}, { 
  tags: ['country:it'], // keep track of the current country
});

Then we can send a notification to all the subscribers that have that tag:

notification = Pushpad::Notification.new({
  body: "Ciao Italia!"
})

# deliver to segments
notification.broadcast tags: ['country:it']