A Node.js client library for OneSignal API.
npm install onesignal-node --save
var OneSignal = require('onesignal-node');
You can create a OneSignal Client as shown below. It takes a JSON object as parameter which
contains your OneSignal API credentials.
You can find your userAuthKey and REST API Key (appAuthKey) on OneSignal Account & API Keys
page.
// create a new Client for a single app
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
// note that "app" must have "appAuthKey" and "appId" keys
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
You can also create a Client for multiple Apps
// create a Client for a multiple apps
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
apps: ['id1', 'id2'] // your app ids
});
You can always create a Client with no credential and set them later:
// create a Client for a multiple apps
var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';
myClient.app = { appAuthKey: 'XXXXX', appId: 'XXXXX' };
// or
myClient.setApp({ appAuthKey: 'XXXXX', appId: 'XXXXX' });
myClient.apps = ['id1', 'id2', 'id3']; // this will override "app"
We will pass Notification objects to the Client object to send them.
// contents is REQUIRED unless content_available=true or template_id is set.
var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
});
You can also create a Notification object without contents:
var firstNotification = new OneSignal.Notification({
content_available: true
});
// or if you want to use template_id instead:
var firstNotification = new OneSignal.Notification({
template_id: "be4a8044-bbd6-11e4-a581-000c2940e62c"
});
You can set filters, data, buttons and all of the fields available on OneSignal Documentation
by using .setParameter(paramName, paramValue)
function:
var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
});
firstNotification.setParameter('data', {"abc": "123", "foo": "bar"});
firstNotification.setParameter('headings', {"en": "English Title", "es": "Spanish Title"});
Sending a notification using Segments:
var OneSignal = require('onesignal-node');
// first we need to create a client
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
// we need to create a notification to send
var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
});
// set target users
firstNotification.setIncludedSegments(['All']);
firstNotification.setExcludedSegments(['Inactive Users']);
// set notification parameters
firstNotification.setParameter('data', {"abc": "123", "foo": "bar"});
firstNotification.setParameter('send_after', 'Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)');
// send this notification to All Users except Inactive ones
myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) {
console.log('Something went wrong...');
} else {
console.log(data, httpResponse.statusCode);
}
});
You can also use Promises:
myClient.sendNotification(firstNotification)
.then(function (response) {
console.log(response.data, response.httpResponse.statusCode);
})
.catch(function (err) {
console.log('Something went wrong...', err);
});
To send a notification based on filters, use .setFilters(filters)
method:
var OneSignal = require('onesignal-node');
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
});
firstNotification.setFilters([
{"field": "tag", "key": "level", "relation": ">", "value": "10"},
{"field": "amount_spent", "relation": ">","value": "0"}
]);
myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) {
console.log('Something went wrong...');
} else {
console.log(data);
}
});
To target one or more device, use .setTargetDevices(include_player_ids)
method:
var OneSignal = require('onesignal-node');
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
});
firstNotification.setTargetDevices(["1dd608f2-c6a1-11e3-851d-000c2940e62c",
"2dd608f2-c6a1-11e3-851d-000c2940e62c"]);
myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) {
console.log('Something went wrong...');
} else {
console.log(data);
}
});
Note that .sendNotification(notification, callback)
function will send the notification to
the app
specified during the creation of Client object. If you want to send notification
to multiple apps, you must set apps
array instead, on Client object:
var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';
myClient.apps = ['id1', 'id2'];
You can cancel a notification simply by calling .cancel(notificationId, callback)
method
// this will cancel the notification for current app (myClient.app)
myClient.cancelNotification('notificationId', function (err, httpResponse, data) {
if (err) {
}
})
To view all push notifications for an app:
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.viewNotifications('limit=30', function (err, httpResponse, data) {
if (httpResponse.statusCode === 200 && !err) {
console.log(data);
}
});
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.viewNotification('notificationId', function (err, httpResponse, data) {
if (httpResponse.statusCode === 200 && !err) {
console.log(data);
}
});
myClient.viewApps(function (err, httpResponse, data) {
console.log(data[0].name); // print the name of the app
});
you can also view a single app
myClient.viewApp('appId', function (err, httpResponse, data) {
console.log(data);
});
var OneSignal = require('onesignal-node');
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX'
});
var appBody = {
name: 'Test App',
apns_env: 'production',
gcm_key: 'xxxxx-aaaaa-bbbb'
};
myClient.createApp(appBody, function (err, httpResponse, data) {
if (httpResponse.statusCode === 200) {
console.log(data);
}
});
var OneSignal = require('onesignal-node');
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
var appBody = {
name: 'New Test App',
gcm_key: 'xxxxx-aaaaa-bbbb'
};
myClient.updateApp(appBody, function (err, httpResponse, data) {
console.log(data);
});
You can view devices for an app:
var myClient = new OneSignal.Client({
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
// you can set limit and offset (optional) or you can leave it empty
myClient.viewDevices('limit=100&offset=0', function (err, httpResponse, data) {
console.log(data);
});
var myClient = new OneSignal.Client({
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.viewDevice('deviceId', function (err, httpResponse, data) {
console.log(data);
});
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
// If you want to add device to current app, don't add app_id in deviceBody
var deviceBody = {
device_type: 1,
language: 'tr'
};
myClient.addDevice(deviceBody, function (err, httpResponse, data) {
...
});
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
var deviceBody = {
device_type: 0,
language: 'en',
device_model: 'iPhone5,1'
};
myClient.editDevice('deviceId', deviceBody, function (err, httpResponse, data) {
...
});
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.csvExport({ extra_fields: ['location'] }, function (err, httpResponse, data) {
...
});
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.trackOpen('notificationId', { opened: true }, function (err, httpResponse, data) {
...
});
This project is under the MIT license.