Commit 93f5fdbd authored by zeynel's avatar zeynel

Update README for new release

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