0

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

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。那么如何解决这个错误呢?

4

1 回答 1

1

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

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

let notification = new pushpad.Notification({
  project: project,
  body: 'Hello world!'
});
于 2018-05-15T11:21:27.483 回答