web-pushpush-apipushpad

Pushpad is not able to send notifcation from node.js app


I have written the code shown below to send the push notification:

const pushpad = require('pushpad');
require('dotenv').config();
const axios = require('axios');


const project = new pushpad.Pushpad({
    authToken: process.env.PUSH_PAD_AUTH_TOKEN,
    projectId: process.env.PUSH_PAD_PROJECT_ID
});


console.log('called pushpad');



let notification = new pushpad.Notification({
    project: project,
    body: 'Hello world!',
    title: 'Website Name',
    targetUrl: 'http://example.com',
    iconUrl: 'http://example.com/assets/icon.png',
    imageUrl: 'http://example.com/assets/image.png',
    ttl: 604800,
    requireInteraction: true,
    customData: '123',
    actions: [
        {
            title: 'My Button 1',
            targetUrl: 'http://example.com/button-link',
            icon: 'http://example.com/assets/button-icon.png',
            action: 'myActionName'
        }
    ],
    starred: true,
    sendAt: new Date(Date.UTC(2016, 7 - 1, 25, 10, 9)),
    customMetrics: ['examples', 'another_metric']
});


// deliver to everyone
notification.broadcast(function (err, result) {
    console.log("error is " + err);
});


module.exports = notification;

but somehow, as soon as I run this code, it gives me error Unprocessable Entity . So how to solve this error ?


Solution

  • Probably you have entered some wrong parameters. For example customMetrics must be defined in your project settings before using them.

    Try the same code with a simple notification:

    let notification = new pushpad.Notification({
      project: project,
      body: 'Hello world!'
    });