Commit 93f5fdbd authored by zeynel's avatar zeynel

Update README for new release

parent e3a801df
# onesignal-node # onesignal-node
A Node.js client library for [OneSignal](https://onesignal.com/) API. A Node.js client library for [OneSignal](https://onesignal.com/) API.
...@@ -67,17 +68,18 @@ myClient.apps = ['id1', 'id2', 'id3']; // this will override "app" ...@@ -67,17 +68,18 @@ myClient.apps = ['id1', 'id2', 'id3']; // this will override "app"
### Creating new notification object ### Creating new notification object
We will pass Notification objects to the Client object 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 = new OneSignal.Notification({ var firstNotification = new OneSignal.Notification({
contents: { contents: {
en: "Test notification", en: "Test notification",
tr: "Test mesajı" tr: "Test mesajı"
} },
included_segments: ["Active Users", "Inactive Users"]
}); });
``` ```
You can also create a Notification object without contents: You can also create a Notification object without contents:
``` js ```js
var firstNotification = new OneSignal.Notification({ var firstNotification = new OneSignal.Notification({
content_available: true content_available: true
}); });
...@@ -89,21 +91,25 @@ var firstNotification = new OneSignal.Notification({ ...@@ -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) 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 `postBody` JSON variable.
``` js ```js
var firstNotification = new OneSignal.Notification({ var firstNotification = new OneSignal.Notification({
contents: { contents: {
en: "Test notification", en: "Test notification",
tr: "Test mesajı" 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 Push Notifications
Sending a notification using Segments: Sending a notification using Segments:
``` js ```js
var OneSignal = require('onesignal-node'); var OneSignal = require('onesignal-node');
// first we need to create a client // first we need to create a client
...@@ -121,12 +127,12 @@ var firstNotification = new OneSignal.Notification({ ...@@ -121,12 +127,12 @@ var firstNotification = new OneSignal.Notification({
}); });
// set target users // set target users
firstNotification.setIncludedSegments(['All']); firstNotification.postBody["included_segments"] = ["Active Users"];
firstNotification.setExcludedSegments(['Inactive Users']); firstNotification.postBody["excluded_segments"] = ["Banned Users"];
// set notification parameters // set notification parameters
firstNotification.setParameter('data', {"abc": "123", "foo": "bar"}); firstNotification.postBody["data"] = {"abc": "123", "foo": "bar"};
firstNotification.setParameter('send_after', 'Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)'); firstNotification.postBody["send_after"] = 'Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)';
// send this notification to All Users except Inactive ones // send this notification to All Users except Inactive ones
myClient.sendNotification(firstNotification, function (err, httpResponse,data) { myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
...@@ -150,8 +156,8 @@ myClient.sendNotification(firstNotification) ...@@ -150,8 +156,8 @@ myClient.sendNotification(firstNotification)
}); });
``` ```
To send a notification based on filters, use `.setFilters(filters)` method: To send a notification based on filters, use `filters` parameter:
``` js ```js
var OneSignal = require('onesignal-node'); var OneSignal = require('onesignal-node');
var myClient = new OneSignal.Client({ var myClient = new OneSignal.Client({
...@@ -163,13 +169,16 @@ var firstNotification = new OneSignal.Notification({ ...@@ -163,13 +169,16 @@ var firstNotification = new OneSignal.Notification({
contents: { contents: {
en: "Test notification", en: "Test notification",
tr: "Test mesajı" tr: "Test mesajı"
} },
}); filters: [
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"}
]); ]
});
// 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) { myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) { if (err) {
...@@ -179,8 +188,8 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,data) { ...@@ -179,8 +188,8 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
} }
}); });
``` ```
To target one or more device, use `.setTargetDevices(include_player_ids)` method: To target one or more device, use `include_player_ids` parameter:
``` js ```js
var OneSignal = require('onesignal-node'); var OneSignal = require('onesignal-node');
var myClient = new OneSignal.Client({ var myClient = new OneSignal.Client({
...@@ -192,11 +201,12 @@ var firstNotification = new OneSignal.Notification({ ...@@ -192,11 +201,12 @@ var firstNotification = new OneSignal.Notification({
contents: { contents: {
en: "Test notification", en: "Test notification",
tr: "Test mesajı" tr: "Test mesajı"
} },
include_player_ids: ["1dd608f2-c6a1-11e3-851d-000c2940e62c", "2dd608f2-c6a1-11e3-851d-000c2940e62c"]
}); });
firstNotification.setTargetDevices(["1dd608f2-c6a1-11e3-851d-000c2940e62c", // Add a new target after creating initial notification body
"2dd608f2-c6a1-11e3-851d-000c2940e62c"]); firstNotification.postBody["include_player_ids"].push["3aa608f2-c6a1-11e3-851d-000c2940e62c"]
myClient.sendNotification(firstNotification, function (err, httpResponse,data) { myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
if (err) { if (err) {
...@@ -211,7 +221,7 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,data) { ...@@ -211,7 +221,7 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,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
to 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
var myClient = new OneSignal.Client({}); var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX'; myClient.userAuthKey = 'XXXXXX';
myClient.apps = ['id1', 'id2']; myClient.apps = ['id1', 'id2'];
...@@ -219,7 +229,7 @@ myClient.apps = ['id1', 'id2']; ...@@ -219,7 +229,7 @@ myClient.apps = ['id1', 'id2'];
### Cancelling a push notification ### Cancelling a push notification
You can cancel a notification simply by calling `.cancel(notificationId, callback)` method 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) {
if (err) { if (err) {
...@@ -231,7 +241,7 @@ myClient.cancelNotification('notificationId', function (err, httpResponse, data) ...@@ -231,7 +241,7 @@ myClient.cancelNotification('notificationId', function (err, httpResponse, data)
### Viewing push notifications ### Viewing push notifications
To view all push notifications for an app: To view all push notifications for an app:
``` 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' }
......
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