【问题标题】:Error while sending web-push notifications from node API deployed on Azure Server从部署在 Azure 服务器上的节点 API 发送 Web 推送通知时出错
【发布时间】:2021-10-23 21:58:17
【问题描述】:

我创建了一个 API,它将订阅通知并通过通知通知所有订阅的客户端。它在本地运行良好,但是一旦我将它部署到 Azure,它就无法将通知推送到客户端。我是否缺少某些必须阻止来自服务器的外部通知的东西?任何帮助表示赞赏。

注意:NodeAPI 工作得非常好,我得到了正确的响应,只是没有发送通知或必须阻止通知。

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");
});


【问题讨论】:

    标签: node.js angular azure azure-web-app-service web-push


    【解决方案1】:

    感谢@sabbour的回答

    除了 NodeJS 通过 azure 推送通知,您可以将 Azure Notification Hub SDK 用于 NodeJS。

    NodeJS应用中推送通知的实现参考这里notification-hubs-nodejs-push-notification-tutorial

    更多信息请参考here 12

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 2019-10-30
      • 2012-12-23
      • 2018-09-22
      • 2017-09-07
      • 2014-12-31
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多