Setting up Azure Push Notifications for React Web App with Node Express Backend

Rohit 0 Reputation points
2024-12-30T06:31:38.93+00:00

Hi Azure Team,

I am looking to implement push notifications in my existing React web application that is using a Node Express backend. I would like to integrate Azure Push Notification Service (or any other relevant Azure service) to handle push notifications for this web app.

Could you please guide me on how to set this up? Specifically, I need help with the following:

  1. What Azure services should I use for push notifications in a web app (e.g., Azure Notification Hubs, Azure Communication Services, etc.)?
  2. How to set up Azure Push Notifications to work with my React frontend?
  3. How to configure and send notifications from the Node Express backend to the React app?
  4. Any best practices for ensuring secure and reliable push notifications in this setup?
  5. If there are any tutorials or examples available for integrating Azure Push Notifications with React and Node.js, that would be very helpful.

Looking forward to your response. Thank you!

Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
338 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shree Hima Bindu Maganti 1,615 Reputation points Microsoft Vendor
    2025-01-02T17:40:45.43+00:00

    Hi Rohit
    Welcome to the Microsoft Q&A Platform!
    setting up push notifications in your React web application with a Node Express backend using Azure Notification Hubs.
    Use Azure Notification Hubs for push notifications.

    Generate VAPID keys using the web-push library.
    Go to Azure Portal >Create a Notification Hub >Set up a Namespace.

    Generate VAPID keys
    npx web-push generate-vapid-keys
    Add VAPID public/private keys to Notification Hub settings under Push Notifications.
    Set Up Service Worker (sw.js).

    self.addEventListener('push', (event) => {
        const data = event.data.json();
        self.registration.showNotification(data.title, {
            body: data.body,
            icon: data.icon,
        });
    });
    

    Request Notification Permissions:

    async function subscribeUser() {
        const registration = await navigator.serviceWorker.register('/sw.js');
        const subscription = await registration.pushManager.subscribe({
            userVisibleOnly: true,
            applicationServerKey: '<Your VAPID Public Key>',
        });
        await fetch('/api/save-subscription', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(subscription),
        });
    }
    

    Replace <Your VAPID Public Key> with the public key from Azure Notification Hubs.
    npm install web-push body-parser

    Save and Send Notifications.

    const express = require('express');
    const webPush = require('web-push');
    const bodyParser = require('body-parser');
    const app = express();
    app.use(bodyParser.json());
    const publicVapidKey = '<Your VAPID Public Key>';
    const privateVapidKey = '<Your VAPID Private Key>';
    webPush.setVapidDetails('mailto:you@example.com', publicVapidKey, privateVapidKey);
    let subscriptions = [];
    app.post('/api/save-subscription', (req, res) => {
        subscriptions.push(req.body);
        res.status(201).json({});
    });
    app.post('/api/send-notification', (req, res) => {
        const payload = JSON.stringify({ title: 'Hello!', body: 'This is a notification.' });
        subscriptions.forEach((sub) =>
            webPush.sendNotification(sub, payload).catch(console.error)
        );
        res.status(200).json({ message: 'Notifications sent' });
    });
    app.listen(3001, () => console.log('Server running on port 3001'));
    

    https://zcusa.951200.xyz/en-us/azure/notification-hubs/
    https://developer.mozilla.org/en-US/docs/Web/API/Push_API
    If the answer is helpful, please click ACCEPT ANSWER and kindly upvote it .

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.