我正在尝试使用node.js的网络推送插件在我的web应用程序上实现推送通知。
// server.js
var webPush = require('web-push');
webPush.setGCMAPIKey("MY_API_KEY"); // I replace this with my key
router.post('/sendNotification', function(req, res) {
webPush.sendNotification({ endpoint: req.query.endpoint }, "Payload test here");
});我没有错误,但我没有收到任何通知.
// index.js
var endpoint;
navigator.serviceWorker.register('service-worker.js')
.then(function(registration) {
return registration.pushManager.getSubscription()
.then(function(subscription) {
if (subscription) {
return subscription;
}
return registration.pushManager.subscribe({ userVisibleOnly: true });
});
}).then(function(subscription) {
endpoint = subscription.endpoint;
}
document.getElementById('doIt').onclick = function() {
fetch(
'./sendNotification?endpoint=' + endpoint,
{ method: 'post' }
);
};。
// service-worker.js
self.addEventListener('push', function(event) {
event.waitUntil(
self.registration.showNotification('header test', {
body: 'body test',
})
);
});因此,当我单击btn (#doIt)时,服务器路由/sendNotification将被很好地调用,具有正确的端点,但是.没有通知发生!
希望你能帮我!提前谢谢!
发布于 2018-05-16 15:26:22
您不一定需要GCM密钥,但您确实需要汽化键,请参阅https://www.npmjs.com/package/web-push#usage。确保你有乏味的钥匙打给webPush.setVapidDetails()。如果你要找一个更详细的指南,我写了一个用Node.js在网络推送上发布博客。
https://stackoverflow.com/questions/45012642
复制相似问题