Commit 66b13a0e authored by ='s avatar =

add new function documentations

parent e5d6f9d3
...@@ -7,15 +7,23 @@ A Node.js client library for [OneSignal](https://onesignal.com/) API. ...@@ -7,15 +7,23 @@ A Node.js client library for [OneSignal](https://onesignal.com/) API.
* [Usage](*usage) * [Usage](*usage)
* [Creating a service client](#creating-a-client) * [Creating a service client](#creating-a-client)
* [Sending a push notification](#sending-push-notifications) * [Sending a push notification](#sending-push-notifications)
* [Canceling a push notification](#canceling-a-push-notification) * [Cancelling a push notification](#cancelling-a-push-notification)
* [Viewing push notifications](#viewing-push-notifications)
* [Viewing a push notification](#viewing-a-push-notification)
* [Listing apps](#viewing-apps)
* [Creating an app](#creating-an-app)
* [Updating an app](#updating-an-app)
* [Listing devices](#viewing-devices)
* [Vieving a device](#viewing-a-device)
* [Adding a device](#adding-a-device)
* [Editing a device](#editing-a-device)
## Installation ## Installation
``` ```
npm install onesignal-node --save npm install onesignal-node --save
``` ```
##Usage ## Usage
``` js ``` js
var OneSignal = require('onesignal-node'); var OneSignal = require('onesignal-node');
``` ```
...@@ -23,8 +31,9 @@ var OneSignal = require('onesignal-node'); ...@@ -23,8 +31,9 @@ var OneSignal = require('onesignal-node');
### Creating a client ### Creating a client
You can create a OneSignal Client as shown below. It takes a JSON object as parameter which You can create a OneSignal Client as shown below. It takes a JSON object as parameter which
contains your OneSignal API credentials. contains your OneSignal API credentials.
You can find your userAuthKey and REST API Key (appAuthKey) on OneSignal `Account & API Keys` page.
``` js ``` js
// create a Client for a single app // create a new Client for a single app
var myClient = new OneSignal.Client({ var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX', userAuthKey: 'XXXXXX',
// note that "app" must have "appAuthKey" and "appId" keys // note that "app" must have "appAuthKey" and "appId" keys
...@@ -54,10 +63,10 @@ myClient.apps = ['id1', 'id2', 'id3']; // this will override "app" ...@@ -54,10 +63,10 @@ myClient.apps = ['id1', 'id2', 'id3']; // this will override "app"
``` ```
### Creating new notification object ### Creating new notification object
We will pass Notification objects to Client to send them. We will pass Notification objects to the Client object to send them.
``` js ``` js
// contents is REQUIRED unless content_available=true or template_id is set. // contents is REQUIRED unless content_available=true or template_id is set.
var firstNotification = OneSignal.Notification({ var firstNotification = new OneSignal.Notification({
contents: { contents: {
en: "Test notification", en: "Test notification",
tr: "Test mesajı" tr: "Test mesajı"
...@@ -66,12 +75,12 @@ var firstNotification = OneSignal.Notification({ ...@@ -66,12 +75,12 @@ var firstNotification = OneSignal.Notification({
``` ```
You can also create a Notification object without contents: You can also create a Notification object without contents:
``` js ``` js
var firstNotification = OneSignal.Notification({ var firstNotification = new OneSignal.Notification({
content_available: true content_available: true
}); });
// or if you want to use template_id instead: // or if you want to use template_id instead:
var firstNotification = OneSignal.Notification({ var firstNotification = new OneSignal.Notification({
template_id: "be4a8044-bbd6-11e4-a581-000c2940e62c" template_id: "be4a8044-bbd6-11e4-a581-000c2940e62c"
}); });
``` ```
...@@ -79,14 +88,20 @@ var firstNotification = OneSignal.Notification({ ...@@ -79,14 +88,20 @@ var firstNotification = OneSignal.Notification({
You can set filters, data, buttons and all of the fields available on [OneSignal Documentation](https://documentation.onesignal.com/reference#create-notification) 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: by using `.setParameter(paramName, paramValue)` function:
``` js ``` js
var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
});
firstNotification.setParameter('data', {"abc": "123", "foo": "bar"}); firstNotification.setParameter('data', {"abc": "123", "foo": "bar"});
firstNotification.setParameter('headings', {"en": "English Title", "es": "Spanish Title"}); firstNotification.setParameter('headings', {"en": "English Title", "es": "Spanish Title"});
``` ```
### Sending Push Notifications ### Sending Push Notifications
Sending a notification using using Segments: Sending a notification using Segments:
``` js ``` js
var OneSignal = require('../lib'); var OneSignal = require('onesignal-node');
// first we need to create a client // first we need to create a client
var myClient = new OneSignal.Client({ var myClient = new OneSignal.Client({
...@@ -95,7 +110,7 @@ var myClient = new OneSignal.Client({ ...@@ -95,7 +110,7 @@ var myClient = new OneSignal.Client({
}); });
// we need to create a notification to send // we need to create a notification to send
var firstNotification = OneSignal.Notification({ var firstNotification = new OneSignal.Notification({
contents: { contents: {
en: "Test notification", en: "Test notification",
tr: "Test mesajı" tr: "Test mesajı"
...@@ -115,33 +130,80 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,data) { ...@@ -115,33 +130,80 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) { if (err) {
console.log('Something went wrong...'); console.log('Something went wrong...');
} else { } else {
console.log(data); console.log(data, httpResponse.statusCode);
} }
}); });
``` ```
To send a notification based on filters, use `.setFilter(filters)` method: To send a notification based on filters, use `.setFilters(filters)` method:
``` js ``` 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ı"
}
});
firstNotification.setFilters([ firstNotification.setFilters([
{"field": "tag", "key": "level", "relation": ">", "value": "10"}, {"field": "tag", "key": "level", "relation": ">", "value": "10"},
{"field": "amount_spent", "relation": ">","value": "0"} {"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 send specific devices use `.setTargetDevices(include_player_ids)` method: To target one or more devices use `.setTargetDevices(include_player_ids)` method:
``` js ``` 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ı"
}
});
firstNotification.setTargetDevices(["1dd608f2-c6a1-11e3-851d-000c2940e62c", firstNotification.setTargetDevices(["1dd608f2-c6a1-11e3-851d-000c2940e62c",
"2dd608f2-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 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 the `app` specified during the creation of Client object. If you want to send notification
tp multiple apps, you must set `apps` array instead, on Client object: to multiple apps, you must set `apps` array instead, on Client object:
``` js ``` js
myClient.apps = apps: ['id1', 'id2']; var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';
myClient.apps = ['id1', 'id2'];
``` ```
### Canceling a push notification ### Cancelling a push notification
You can cancel a notification simply by calling `.cancel` You can cancel a notification simply by calling `.cancel(notificationId, callback)` method
``` js ``` js
// this will cancel the notification for current app (myClient.app) // this will cancel the notification for current app (myClient.app)
myClient.cancelNotification('notificationId', function (err, httpResponse, data) { myClient.cancelNotification('notificationId', function (err, httpResponse, data) {
...@@ -151,6 +213,152 @@ myClient.cancelNotification('notificationId', function (err, httpResponse, data) ...@@ -151,6 +213,152 @@ myClient.cancelNotification('notificationId', function (err, httpResponse, data)
}) })
``` ```
### Viewing 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);
}
});
```
### Viewing 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);
}
});
```
### 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) {
...
});
```
##License ##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