【问题标题】:How to send image from back-end in your web push notifications?如何在 Web 推送通知中从后端发送图像?
【发布时间】:2020-02-15 02:25:22
【问题描述】:

我正在使用 web-push 库通过节点服务器发送推送通知。目前,我只能从后端发送标题。有没有办法从后端发送图像?

【问题讨论】:

    标签: javascript node.js push-notification web-push


    【解决方案1】:

    这是发送带有图像的通知的示例函数:

    function sendPushNotification(req, res) {
      const subscriptionId = req.params.id;
      const pushSubscription = subscriptions[subscriptionId];
      webpush
        .sendNotification(
          pushSubscription,
          JSON.stringify({
            title: "your title",
            text: "your text",
            image: "path/to/image.jpg",
            tag: "new...",
            url: "/your-url.html"
          })
        )
        .catch(err => {
          console.log(err);
        });
    
      res.status(202).json({});
    }
    

    这来自 Lorenzo Spyna,它解释了如何在这个 tutorial 中进行操作,您可以在 github 的 this project 中查看所有代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      相关资源
      最近更新 更多