(NodeJS) Web Push Notifications Extension

Hi. Created an extension with web-push to send push notifications to users!


Below the explanation on how to set it up, apologies for the many steps.

Let me know if you have any issues, ideas or optimisations as I’m still very new in extension development.

Download

Functionality

Allows to send push notifications to users from server connect api’s, you can set it up to only send a notification if user is not active in the tab / page.

This means that the notification will also work when your page is closed, great way to remind users to check something on your app!

Requirements

Installation

1. Add files from package

2. Add this script tag in your layout page before closing body tag

<script src="/client.js"></script>

3. Generate Vapid Public & Private Keys

Generate the keys with the “Generate Vapid Keys” action from a temporary server connect api. Click on open in browser to copy the generated keys

Keep the page open for now as you will copy and paste these keys below, you can delete this server connect api once the steps below are completed

4. Open web_push.js (Added in step 1), paste the public & private keys

5. Open client.js (Added in step 1), paste ONLY public key

Paste the public key in the area.

Below that, you can see that the “show_only_when_tab_not_active” is set on true, this means that the user will only get notifications when your page is not active / open in the user’s tab. If you always want to push the notification, even when user is active on the page, you can set this setting to false

6. Set up server connect api to get subscriber object

The script sends the user’s subscription object to /api/web_push/subscribe, this means that you must create a folder in your api’s called “web_push” and add an action named “subscribe” in this folder

Screenshot 2022-09-08 at 18.41.02

This “subscribe” action will receive the subscription data with POST.
This subscription object must be connected to the user, to send this user notifications later on. This subscription object will update often so you will need to create a flow to update this.

What I did for the subscription flow:

  1. Create a database table called “push_subscriptions” with a user_id reference field and a json field for the subscription object
    Screenshot 2022-09-08 at 18.48.44

  2. In the newly created “subscribe” API action, add a security provider “identity” action to get the user’s id

  3. Add a database query to check if this user has a subscription in the newly created “push_subscriptions” table

  4. Add a condition action, if the query returns a result (when it’s true), create an update action to update this user’s subscription object in the “push_subscriptions” table, use the whole “$_POST” as a value (This contains the whole subscription object that is needed)

  5. Add an else action to this condition, to insert a new record in the “push_subscriptions” with the user_id and the “$_POST” value for the subscription
    Screenshot 2022-09-08 at 19.00.57

7. Send Push Notification!

  1. You can now use the “Send notification” action in any server connect api
  2. You can query the “push_subscriptions” table in the database to get the user subscription object that you’d like to send a notification to (To send a notification to everyone you can simply query the whole table and repeat the records)

    Subscription = Add the subscription object here
    Title = Title text in notification (string)
    Body = Body text in notification (string)
    Icon = Url of image / icon to show (string)

Result:

Additional Comments

Community Page
Last updated: