Skip to main content

Push Notifications

Push messages enable your gateway to send notifications on desktop and mobile devices even when the users are not active.

To implement push notifications, you need to:

  1. Create a Service Worker
  2. Ask the user for permission to send push notifications
  3. Send the client identifier information to our notification server
  4. Add logic to display the notifications
Example

Create the Service Worker

Push notifications work by having a service worker on the client side that listens for messages from the NEAR notifications server.

app/scripts/main.js
loading...

Browsers readily provide native support for service workers, so you can easily check if a service worker exists, and create one if not.


Subscribe to our Notifications

In order to have the service worker display notifications, you need to subscribe it to a notifications server.

A notification server is identified by its public key, constraining that only the server holding the private counterpart can push notifications to the user.

app/scripts/main.js
loading...
Permission

When you subscribe to the service, the user will be asked for permission to be sent notifications.


Create a Stream in our Server

After you subscribe the user to a notifications server, share it with us so we can start sending you notifications!

For this, make a post request to our server, add which account you want to be notified for, and a URL identifying your gateway.

app/scripts/main.js
loading...
tip

The gateway parameter is there just to help us keep track of who receives notifications.


Handle Notifications

When the user receives a notification, the service worker will be triggered, and you can add logic to display the notification.

app/scripts/sw.js
loading...

Feel free to personalize the notification as you wish, and to add logic on what to do once the notification is clicked. In our example, we just open the Post page.

app/scripts/sw.js
loading...
Was this page helpful?