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

时间:2018-05-15 10:01:53

标签: web-push push-api pushpad

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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