Search code examples
node.jspush-notificationweb-push

How to make your own push notification service for your website


I am working on a website, where I want to make a feature of notifications, when a user visits my website, they are asked for notifications permission and when they allow it, they will get notifications from my website, and whatever product I want them to get notified by.

Like for example when I visit some websites, they ask me for notifications permissions and when I allow the, I get notified through notifications then. That's all I want for now.

How can I achieve this functionality, I have follow this tutorial, but still confused how the users who allowed the notifications get detected and how all of them are notified then ?


Solution

  • Web Push library for node.js is just a sender.

    You should obtain a subscription JSON object from the browser using Notification API and Service Worker API and then send it to your server, where put it in the database of your choise.

    When you will need broadcast notifications, you can retrieve subscription and use a web-push library (for php is also available :)

    Note that is a right flow looks following as: 1) Retrieve subscription from the browser 2) Send and store it on your web-server 3) Create notification prototype (just object) 4) Broadcast notification prototype ID you have created to the users 5) Service Worker receive one and fetch notification prototype from your server by ID 6) Show notification using browser API in service worker

    For more see here links

    https://developers.google.com/web/fundamentals/codelabs/push-notifications/ https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications/