How to send a pushengage notification with node-fetch

Send push notifications using pushengage.com through Node.js and node-fetch module


Andres Canavesi
Jan 14, 2022
Sending push notifications from pushengage.com dashboard is pretty easy but what about f we want to automate some process in our web site?
featured image

Context

Sending push notifications from pushengage.com dashboard is pretty easy but what about f we want to automate some process in our web site? For instance, let say we want to send a push notification every time you create a blog post

The code

      

  const baseUrl = 'https://api.pushengage.com/apiv1/notifications';
  const params = {
    title: '[notification title here]',
    message: '[notification message here]',
    url: '[notification url here]',
    imageUrl: '[your image url here]',
  };
  const requestParams = `notification_type=draft&notification_title=${params.title}&notification_message=${params.message}&notification_url=${params.url}&image_url=${params.imageUrl}`;
  const url = baseUrl;
  const res = await fetch(url,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        api_key: [your_api_key_here],
      },
      body: requestParams,
    });
  const body = await res.text();
  console.info(res.status);
  console.info(body);

      
    
Note: have in mind we are sending the notification as a draft: notification_type=draft. Once ready, remove that parameter to send the real notification

Output

The first console log will print 200 status code and the second one the operation result as json
      
200
{"success":true,"notification_id":13623215772}