0

I have created an API that will subscribe for notification and notify all the subscribed clients with a notification. It worked locally fine, but once I deployed it to Azure it is not able to push the notifications to Clients. Is there something I am missing which must be blocking the outward notification from the server? Any help is appreciated.

Note: The NodeAPI is working perfectly fine, and I get proper responses, just notifications are not being sent or must be blocking.

const express = require('express');
const app = express();
const port =8080;

const webpush = require("web-push");
const publicKey = ""; \\blacked out for security purpose
const privateKey = ""; \\blacked out for security purpose
webpush.setVapidDetails("mailto:example@yourdomain.org",publicKey,privateKey);

const notificationClient = [];

app.use(express.json());
app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});
app.listen(port,()=> console.log("listening to port:"+ port));

app.get('/',(req,res)=>{
  res.status(200).send("API is Running");
});

app.get('/api/notify',(req,res)=>{
    const payload = {
        notification:{
          title: "Message From Heaven ",
          icon: '/assets/icons/icon-128x128.png',
          body: 'We beleive in you.\n Do your deeds good ',
          data:{ url:"https://google.co.in"},
          vibrate: [500,110,500,110,450,110,200,110,170,40,450,110,200,110,170,40,500]
        }
      };
      notificationClient.forEach(clientSub => {
          console.log(clientSub);
        webpush.sendNotification(clientSub, JSON.stringify(payload));      
      });
    res.status(200).send("Success: ClientsNotified: "+ notificationClient.length);
});

app.post('/api/subscribeNotification',(req,res)=>{
    console.log(req);
    // const  {id} = req.params;
    // const  {Sub} = req.body;
    const clientSub = req.body;
    notificationClient.push(clientSub);
    res.status(200).send("Notification Subscribed");
});


Pratik Bhoir
  • 2,074
  • 7
  • 19
  • 36

1 Answers1

0

Thanks @ sabbour for your answer

Instead of NodeJS push notification via azure you can use the Azure Notification Hub SDK for NodeJS.

Refer here for implement the Push notification in NodeJS application notification-hubs-nodejs-push-notification-tutorial

Refer for more info here 1 and 2

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15