【发布时间】: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