Pushpad 无法从 node.js 应用程序发送通知

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

我已经编写了如下所示的代码来发送推送通知:

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;

但不知何故,一旦我 运行 这段代码,它就会给我错误 Unprocessable Entity 。那么如何解决这个错误呢?

可能是您输入了一些错误的参数。例如 customMetrics 必须在使用它们之前在您的项目设置中定义。

使用简单的通知尝试相同的代码:

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