Search code examples
javascriptnode.jspush-notificationweb-push

How to send image from back-end in your web push notifications?


I am using web-push library to send push notification using node server. For now, I am only able to send title from the back-end. Is there any way to send image from the back end?


Solution

  • This is example function that send a notification with image:

    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({});
    }
    

    this is from Lorenzo Spyna that explains how do it in this tutorial and you can see all code in this project of github