Commit 907fb355 authored by zeynel's avatar zeynel

Update README for new endpoints

parent 74f788e4
# onesignal-node
A Node.js client library for [OneSignal](https://onesignal.com/) API. # onesignal-node
## Table of Contents A Node.js client library for [OneSignal](https://onesignal.com/) API.
* [Installation](#installation)
* [Usage](*usage) ## Table of Contents
* [Creating a service client](#creating-a-client) * [Installation](#installation)
* [Sending a push notification](#sending-push-notifications) * [Usage](*usage)
* [Cancelling a push notification](#cancelling-a-push-notification) * [Creating a service client](#creating-a-client)
* [Viewing push notifications](#viewing-push-notifications) * [Send a push notification](#send-push-notifications)
* [Viewing a push notification](#viewing-a-push-notification) * [Cancel a push notification](#cancel-a-push-notification)
* [Listing apps](#viewing-apps) * [View push notifications](#view-push-notifications)
* [Creating an app](#creating-an-app) * [View a push notification](#view-a-push-notification)
* [Updating an app](#updating-an-app) * [View apps](#view-apps)
* [Listing devices](#viewing-devices) * [Create an app](#create-an-app)
* [Viewing a device](#viewing-a-device) * [Update an app](#update-an-app)
* [Adding a device](#adding-a-device) * [View devices](#view-devices)
* [Editing a device](#editing-a-device) * [View a device](#view-a-device)
* [CSV Export](#csv-export) * [Add a device](#add-a-device)
* [Opening track](#opening-track) * [Edit a device](#edit-a-device)
* [Tests](#tests) * [New session](#new-session)
* [New purchase](#new-purchase)
## Installation * [Increment Session Length](#increment-session-length)
* [CSV Export](#csv-export)
``` * [Open track](#open-track)
npm install onesignal-node --save * [Tests](#tests)
```
## Usage ## Installation
``` js
var OneSignal = require('onesignal-node'); ```
``` npm install onesignal-node --save
```
### Creating a client ## Usage
You can create a OneSignal Client as shown below. It takes a JSON object as parameter which ``` js
contains your OneSignal API credentials. var OneSignal = require('onesignal-node');
You can find your userAuthKey and REST API Key (appAuthKey) on OneSignal `Account & API Keys` page. ```
``` js
// create a new Client for a single app ### Creating a client
var myClient = new OneSignal.Client({ You can create a OneSignal Client as shown below. It takes a JSON object as parameter which
userAuthKey: 'XXXXXX', contains your OneSignal API credentials.
// note that "app" must have "appAuthKey" and "appId" keys You can find your userAuthKey and REST API Key (appAuthKey) on OneSignal `Account & API Keys` page.
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' } ```js
}); // create a new Client for a single app
``` var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
You can also create a Client for multiple Apps // note that "app" must have "appAuthKey" and "appId" keys
``` js app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
// create a Client for a multiple apps });
var myClient = new OneSignal.Client({ ```
userAuthKey: 'XXXXXX',
apps: ['id1', 'id2'] // your app ids You can also create a Client for multiple Apps
}); ```js
``` // create a Client for a multiple apps
You can always create a Client with no credential and set them later: var myClient = new OneSignal.Client({
``` js userAuthKey: 'XXXXXX',
// create a Client for a multiple apps apps: ['id1', 'id2'] // your app ids
var myClient = new OneSignal.Client({}); });
myClient.userAuthKey = 'XXXXXX'; ```
You can always create a Client with no credential and set them later:
myClient.app = { appAuthKey: 'XXXXX', appId: 'XXXXX' }; ```js
// or // create a Client for a multiple apps
myClient.setApp({ appAuthKey: 'XXXXX', appId: 'XXXXX' }); var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';
myClient.apps = ['id1', 'id2', 'id3']; // this will override "app"
``` myClient.app = { appAuthKey: 'XXXXX', appId: 'XXXXX' };
// or
### Creating new notification object myClient.setApp({ appAuthKey: 'XXXXX', appId: 'XXXXX' });
We will pass Notification objects to the Client object to send them.
```js myClient.apps = ['id1', 'id2', 'id3']; // this will override "app"
// contents is REQUIRED unless content_available=true or template_id is set. ```
var firstNotification = new OneSignal.Notification({
contents: { ### Creating new notification object
en: "Test notification", We will pass Notification objects to the Client object to send them.
tr: "Test mesajı" ```js
}, // contents is REQUIRED unless content_available=true or template_id is set.
included_segments: ["Active Users", "Inactive Users"] var firstNotification = new OneSignal.Notification({
}); contents: {
``` en: "Test notification",
You can also create a Notification object without contents: tr: "Test mesajı"
```js },
var firstNotification = new OneSignal.Notification({ included_segments: ["Active Users", "Inactive Users"]
content_available: true });
}); ```
You can also create a Notification object without contents:
// or if you want to use template_id instead: ```js
var firstNotification = new OneSignal.Notification({ var firstNotification = new OneSignal.Notification({
template_id: "be4a8044-bbd6-11e4-a581-000c2940e62c" content_available: true
}); });
```
// or if you want to use template_id instead:
You can set filters, data, buttons and all of the fields available on [OneSignal Documentation](https://documentation.onesignal.com/reference#create-notification) var firstNotification = new OneSignal.Notification({
by using `postBody` JSON variable. template_id: "be4a8044-bbd6-11e4-a581-000c2940e62c"
```js });
var firstNotification = new OneSignal.Notification({ ```
contents: {
en: "Test notification", You can set filters, data, buttons and all of the fields available on [OneSignal Documentation](https://documentation.onesignal.com/reference#create-notification)
tr: "Test mesajı" by using `postBody` JSON variable.
}, ```js
contents: {"en": "Old content"} var firstNotification = new OneSignal.Notification({
}); contents: {
en: "Test notification",
// You can change notification body later by changing postBody tr: "Test mesajı"
firstNotification.postBody["contents"] = {"en": "New content"}; },
firstNotification.postBody["data"] = {"abc": "123", "foo": "bar"}; contents: {"en": "Old content"}
firstNotification.postBody["headings"] = {"en": "English Title", "es": "Spanish Title"}; });
```
// You can change notification body later by changing postBody
### Sending Push Notifications firstNotification.postBody["contents"] = {"en": "New content"};
Sending a notification using Segments:
```js
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.postBody["included_segments"] = ["Active Users"];
firstNotification.postBody["excluded_segments"] = ["Banned Users"];
// set notification parameters
firstNotification.postBody["data"] = {"abc": "123", "foo": "bar"}; firstNotification.postBody["data"] = {"abc": "123", "foo": "bar"};
firstNotification.postBody["send_after"] = 'Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)'; firstNotification.postBody["headings"] = {"en": "English Title", "es": "Spanish Title"};
```
// send this notification to All Users except Inactive ones
myClient.sendNotification(firstNotification, function (err, httpResponse,data) { ### Send Push Notifications
if (err) { Sending a notification using Segments:
console.log('Something went wrong...'); ```js
} else { var OneSignal = require('onesignal-node');
console.log(data, httpResponse.statusCode);
} // first we need to create a client
}); var myClient = new OneSignal.Client({
``` userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
You can also use Promises: });
```js // we need to create a notification to send
myClient.sendNotification(firstNotification) var firstNotification = new OneSignal.Notification({
.then(function (response) { contents: {
console.log(response.data, response.httpResponse.statusCode); en: "Test notification",
}) tr: "Test mesajı"
.catch(function (err) { }
console.log('Something went wrong...', err); });
});
``` // set target users
firstNotification.postBody["included_segments"] = ["Active Users"];
To send a notification based on filters, use `filters` parameter: firstNotification.postBody["excluded_segments"] = ["Banned Users"];
```js
var OneSignal = require('onesignal-node'); // set notification parameters
firstNotification.postBody["data"] = {"abc": "123", "foo": "bar"};
var myClient = new OneSignal.Client({ firstNotification.postBody["send_after"] = 'Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)';
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' } // send this notification to All Users except Inactive ones
}); myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) {
var firstNotification = new OneSignal.Notification({ console.log('Something went wrong...');
contents: { } else {
en: "Test notification", console.log(data, httpResponse.statusCode);
tr: "Test mesajı" }
}, });
filters: [ ```
{"field": "tag", "key": "level", "relation": ">", "value": "10"},
{"field": "amount_spent", "relation": ">","value": "0"} You can also use Promises:
]
}); ```js
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 `filters` parameter:
```js
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ı"
},
filters: [
{"field": "tag", "key": "level", "relation": ">", "value": "10"},
{"field": "amount_spent", "relation": ">","value": "0"}
]
});
// You can change filters later
firstNotification.postBody["filters"] = [{"field": "tag", "key": "level", "relation": ">", "value": "10"}];
firstNotification.postBody["filters"].push({"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 `include_player_ids` parameter:
```js
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ı"
},
include_player_ids: ["1dd608f2-c6a1-11e3-851d-000c2940e62c", "2dd608f2-c6a1-11e3-851d-000c2940e62c"]
});
// Add a new target after creating initial notification body
firstNotification.postBody["include_player_ids"].push["3aa608f2-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:
```js
var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';
myClient.apps = ['id1', 'id2'];
```
### Cancel a push notification
You can cancel a notification simply by calling `.cancel(notificationId, callback)` method
```js
// this will cancel the notification for current app (myClient.app)
myClient.cancelNotification('notificationId', function (err, httpResponse, data) {
if (err) {
}
})
```
### View push notifications
To view all push notifications for an app:
```js
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);
}
});
```
### View a push notification
``` js
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);
}
});
```
### View apps
``` js
myClient.viewApps(function (err, httpResponse, data) {
console.log(data[0].name); // print the name of the app
});
```
you can also view a single app
``` js
myClient.viewApp('appId', function (err, httpResponse, data) {
console.log(data);
});
```
### Create an app
``` js
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);
}
});
```
### Update an app
``` js
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);
});
```
### View devices
You can view devices for an app:
``` js
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);
});
```
### View a device
```js
var myClient = new OneSignal.Client({
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.viewDevice('deviceId', function (err, httpResponse, data) {
console.log(data);
});
```
### Add a device
```js
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) {
...
});
```
### Edit a device
```js
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) {
...
});
```
// You can change filters later ### New Session
firstNotification.postBody["filters"] = [{"field": "tag", "key": "level", "relation": ">", "value": "10"}]; ```js
firstNotification.postBody["filters"].push({"field": "amount_spent", "relation": ">","value": "0"}); var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
myClient.sendNotification(firstNotification, function (err, httpResponse,data) { app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
if (err) { });
console.log('Something went wrong...');
} else { var postBody = {
console.log(data); language: 'es',
} game_version: '1.0',
}); timezone: -28800
``` };
To target one or more device, use `include_player_ids` parameter:
```js myClient.newSession('playerId', postBody, function (err, httpResponse, data) {
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ı"
},
include_player_ids: ["1dd608f2-c6a1-11e3-851d-000c2940e62c", "2dd608f2-c6a1-11e3-851d-000c2940e62c"]
});
// Add a new target after creating initial notification body ### New Purchase
firstNotification.postBody["include_player_ids"].push["3aa608f2-c6a1-11e3-851d-000c2940e62c"] ```js
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
var postBody = {
purchases: [{
sku: 'SKU123',
iso: 'USD',
amount: 0.99
}]
};
myClient.newPurchase('playerId', postBody, function (err, httpResponse, data) {
...
});
```
myClient.sendNotification(firstNotification, function (err, httpResponse,data) { ### Increment Session Length
if (err) { ```js
console.log('Something went wrong...'); var myClient = new OneSignal.Client({
} else { userAuthKey: 'XXXXXX',
console.log(data); app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
} });
});
var postBody = {
``` state: 'ping',
active_time: 60
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: myClient.incrementSessionLength('playerId', postBody, function (err, httpResponse, data) {
```js ...
var myClient = new OneSignal.Client({}); });
myClient.userAuthKey = 'XXXXXX'; ```
myClient.apps = ['id1', 'id2'];
``` ### CSV Export
```js
### Cancelling a push notification var myClient = new OneSignal.Client({
You can cancel a notification simply by calling `.cancel(notificationId, callback)` method userAuthKey: 'XXXXXX',
```js app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
// this will cancel the notification for current app (myClient.app) });
myClient.cancelNotification('notificationId', function (err, httpResponse, data) {
if (err) { myClient.csvExport({ extra_fields: ['location'] }, function (err, httpResponse, data) {
...
} });
}) ```
```
### Open track
### Viewing push notifications ``` js
To view all push notifications for an app: var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
```js app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
var myClient = new OneSignal.Client({ });
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' } myClient.trackOpen('notificationId', { opened: true }, function (err, httpResponse, data) {
}); ...
});
myClient.viewNotifications('limit=30', function (err, httpResponse, data) { ```
if (httpResponse.statusCode === 200 && !err) {
console.log(data); ## Tests
}
}); Running all tests:
``` ```bash
$ npm test
### Viewing a push notification ```
``` js ## License
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);
}
});
```
### Viewing apps
``` js
myClient.viewApps(function (err, httpResponse, data) {
console.log(data[0].name); // print the name of the app
});
```
you can also view a single app
``` js
myClient.viewApp('appId', function (err, httpResponse, data) {
console.log(data);
});
```
### Creating an app
``` js
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);
}
});
```
### Updating an app
``` js
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);
});
```
### Viewing devices
You can view devices for an app:
``` js
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);
});
```
### Viewing a device
``` js
var myClient = new OneSignal.Client({
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.viewDevice('deviceId', function (err, httpResponse, data) {
console.log(data);
});
```
### Adding a device
``` js
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) {
...
});
```
### Editing a device
``` js
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) {
...
});
```
### CSV Export
``` js
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.csvExport({ extra_fields: ['location'] }, function (err, httpResponse, data) {
...
});
```
### Opening track
``` js
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
myClient.trackOpen('notificationId', { opened: true }, function (err, httpResponse, data) {
...
});
```
## Tests
Running all tests:
```bash
$ npm test
```
## License
This project is under the MIT license. This project is under the MIT license.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment