首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的网络推送与node.js,怎么做呢?

简单的网络推送与node.js,怎么做呢?
EN

Stack Overflow用户
提问于 2017-07-10 12:54:04
回答 1查看 2.1K关注 0票数 1

我正在尝试使用node.js的网络推送插件在我的web应用程序上实现推送通知。

代码语言:javascript
复制
// 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");
});

我没有错误,但我没有收到任何通知.

代码语言:javascript
复制
// 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' }
  );
};

代码语言:javascript
复制
// service-worker.js

self.addEventListener('push', function(event) {
  event.waitUntil(
    self.registration.showNotification('header test', {
      body: 'body test',
    })
  );
});

因此,当我单击btn (#doIt)时,服务器路由/sendNotification将被很好地调用,具有正确的端点,但是.没有通知发生!

希望你能帮我!提前谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-05-16 15:26:22

您不一定需要GCM密钥,但您确实需要汽化键,请参阅https://www.npmjs.com/package/web-push#usage。确保你有乏味的钥匙打给webPush.setVapidDetails()。如果你要找一个更详细的指南,我写了一个用Node.js在网络推送上发布博客

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45012642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档