Node.js Backend Sample


Send push notifications to your users with our official Node.js package.

Note: If you don't have an existing Node.js project, consider using our sample Node.js backend project as a starting point to make things easier.


Install the pushy package in your existing project:

npm install pushy --save

Then, use the following sample code to send a push notification to target devices:

var Pushy = require('pushy');
 
// Plug in your Secret API Key 
// Get it here: https://dashboard.pushy.me/ 
var pushyAPI = new Pushy('SECRET_API_KEY');
 
// Set push payload data to deliver to device(s) 
var data = {
    message: 'Hello World!'
};
 
// Insert target device token(s) here 
var to = ['DEVICE_TOKEN'];

// Optionally, send to a publish/subscribe topic instead
// to = '/topics/news';
 
// Set optional push notification options (such as iOS notification fields)
var options = {
    notification: {
        badge: 1,
        sound: 'ping.aiff',
        title: 'Test Notification',
        body: 'Hello World \u270c',
    },
};
 
// Send push notification via the Send Notifications API 
// https://pushy.me/docs/api/send-notifications 
pushyAPI.sendPushNotification(data, to, options, function (err, id) {
    // Log errors to console 
    if (err) {
        return console.log('Fatal Error', err);
    }
    
    // Log success 
    console.log('Push sent successfully! (ID: ' + id + ')');
});

Note: Make sure to replace SECRET_API_KEY with your app's Secret API Key, available in the Pushy Dashboard (Click your app -> API Authentication tab). This is a backend API endpoint. Never expose your application's Secret API Key in your client code.