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.
......@@ -67,17 +68,18 @@ 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
```js
// contents is REQUIRED unless content_available=true or template_id is set.
var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
},
included_segments: ["Active Users", "Inactive Users"]
});
```
You can also create a Notification object without contents:
``` js
```js
var firstNotification = new OneSignal.Notification({
content_available: true
});
......@@ -89,21 +91,25 @@ 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)
by using `.setParameter(paramName, paramValue)` function:
``` js
by using `postBody` JSON variable.
```js
var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
},
contents: {"en": "Old content"}
});
firstNotification.setParameter('data', {"abc": "123", "foo": "bar"});
firstNotification.setParameter('headings', {"en": "English Title", "es": "Spanish Title"});
// You can change notification body later by changing postBody
firstNotification.postBody["contents"] = {"en": "New content"};
firstNotification.postBody["data"] = {"abc": "123", "foo": "bar"};
firstNotification.postBody["headings"] = {"en": "English Title", "es": "Spanish Title"};
```
### Sending Push Notifications
Sending a notification using Segments:
``` js
```js
var OneSignal = require('onesignal-node');
// first we need to create a client
......@@ -121,12 +127,12 @@ var firstNotification = new OneSignal.Notification({
});
// set target users
firstNotification.setIncludedSegments(['All']);
firstNotification.setExcludedSegments(['Inactive Users']);
firstNotification.postBody["included_segments"] = ["Active Users"];
firstNotification.postBody["excluded_segments"] = ["Banned 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)');
firstNotification.postBody["data"] = {"abc": "123", "foo": "bar"};
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) {
......@@ -150,8 +156,8 @@ myClient.sendNotification(firstNotification)
});
```
To send a notification based on filters, use `.setFilters(filters)` method:
``` js
To send a notification based on filters, use `filters` parameter:
```js
var OneSignal = require('onesignal-node');
var myClient = new OneSignal.Client({
......@@ -163,13 +169,16 @@ var firstNotification = new OneSignal.Notification({
contents: {
en: "Test notification",
tr: "Test mesajı"
}
});
firstNotification.setFilters([
},
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) {
......@@ -179,8 +188,8 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
}
});
```
To target one or more device, use `.setTargetDevices(include_player_ids)` method:
``` js
To target one or more device, use `include_player_ids` parameter:
```js
var OneSignal = require('onesignal-node');
var myClient = new OneSignal.Client({
......@@ -192,11 +201,12 @@ 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"]
});
firstNotification.setTargetDevices(["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) {
......@@ -211,7 +221,7 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,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
```js
var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';
myClient.apps = ['id1', 'id2'];
......@@ -219,7 +229,7 @@ myClient.apps = ['id1', 'id2'];
### Cancelling a push notification
You can cancel a notification simply by calling `.cancel(notificationId, callback)` method
``` js
```js
// this will cancel the notification for current app (myClient.app)
myClient.cancelNotification('notificationId', function (err, httpResponse, data) {
if (err) {
......@@ -231,7 +241,7 @@ myClient.cancelNotification('notificationId', function (err, httpResponse, data)
### Viewing push notifications
To view all push notifications for an app:
``` js
```js
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
......
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