pushy-node
The official Node.js package for sending push notifications with Pushy.
Pushy is the most reliable push notification gateway, perfect for real-time, mission-critical applications.
Note: If you don't have an existing Node.js project, consider using our Node.js backend API sample project as a starting point to make things easier for you.
Usage
First, install the package using npm:
npm install pushy --save
Then, use the following code to send a push notification to target devices using the Send Notifications API:
var Pushy = require('pushy');
var pushy = new Pushy('SECRET_API_KEY');
var data = {
message: 'Hello World!'
};
var to = ['DEVICE_TOKEN'];
var options = {
notification: {
badge: 1,
sound: 'ping.aiff',
body: 'Hello World \u270c'
},
};
pushy.sendPushNotification(data, to, options, function (err, result) {
if (err) {
return console.error(err);
}
console.log('Push sent successfully! (ID: ' + result.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).
The library also supports using promise syntax instead of callbacks for all API methods:
pushy.sendPushNotification(data, tokens, options)
.then(function (result) {
console.log('Push sent successfully! (ID: ' + result.id + ')');
}).catch(function (err) {
return console.error(err);
});
Push APIs
pushy.sendPushNotification(data, to, options)
Instantly send push notifications to your users using the Send Notifications API (see example above):
pushy.sendPushNotification(data, to, options, function (err, result) {
if (err) {
return console.error(err);
}
console.log('Push sent successfully! (ID: ' + result.id + ')');
});
pushy.getNotificationStatus(pushId)
Check the delivery status of your push notifications using the Notification Status API:
pushy.getNotificationStatus('PUSH_ID', function (err, status) {
if (err) {
return console.error(err);
}
console.log('Notification Status: ', JSON.stringify(status, null, 2));
});
pushy.deletePushNotification(pushId)
Permanently delete a pending notification using the Notification Deletion API:
pushy.deletePushNotification('PUSH_ID', function (err) {
if (err) {
return console.error(err);
}
console.log('Pending notification deleted successfully');
});
Device APIs
pushy.getDeviceInfo(deviceToken)
Fetch device info, presence, undelivered notifications, and more by device token using the Device Info API:
pushy.getDeviceInfo('DEVICE_TOKEN', function (err, deviceInfo) {
if (err) {
return console.error(err);
}
console.log('Device Info: ', JSON.stringify(deviceInfo, null, 2));
});
pushy.getDevicePresence(deviceTokens)
Check the presence and connectivity status of multiple devices using the Device Presence API:
pushy.getDevicePresence(['DEVICE_TOKEN', 'DEVICE_TOKEN_2'], function (err, devicePresence) {
if (err) {
return console.error(err);
}
console.log('Device Presence: ', JSON.stringify(devicePresence, null, 2));
});
Pub/Sub APIs
pushy.getTopics()
Retrieve a list of your app's topics and subscribers count using the Pub/Sub Topics API:
pushy.getTopics(function (err, topics) {
if (err) {
return console.error(err);
}
console.log('Subscribed topics: \n' + JSON.stringify(topics, null, 2));
});
pushy.getSubscribers(topic)
Retrieve a list of devices subscribed to a certain topic using the Pub/Sub Subscribers API:
pushy.getSubscribers('news', function (err, subscribers) {
if (err) {
return console.error(err);
}
console.log('Devices subscribed to topic: \n' + JSON.stringify(subscribers, null, 2));
});
pushy.subscribe(topics, deviceToken)
Subscribe a device to one or more topics using the Pub/Sub Subscribe API:
pushy.subscribe(['news', 'weather'], 'DEVICE_TOKEN', function (err) {
if (err) {
return console.error(err);
}
console.log('Subscribed device to topic(s) successfully');
});
pushy.unsubscribe(topics, deviceToken)
Unsubscribe a device from one or more topics using the Pub/Sub Unsubscribe API
pushy.unsubscribe(['news', 'weather'], 'DEVICE_TOKEN', function (err) {
if (err) {
return console.error(err);
}
console.log('Unsubscribed device from topic(s) successfully');
});
License
Apache 2.0