Unverified Commit 74f788e4 authored by zeynel's avatar zeynel Committed by GitHub

Merge pull request #13 from zeyneloz/feature/remove-initial-body-restrictions

Feature/remove initial body restrictions
parents 136981b6 eabedd78
# 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' }
......
'use strict';
// TODO Deprecate
var ALLOWED_FIELDS = ['contents', 'included_segments', 'excluded_segments', 'filters', 'include_player_ids',
'app_id', 'app_ids', 'headings', 'subtitle', 'template_id', 'content_available', 'mutable_content',
'data', 'url', 'ios_attachments', 'big_picture', 'adm_big_picture', 'chrome_big_picture', 'buttons',
'web_buttons', 'ios_category', 'android_background_layout', 'small_icon', 'large_icon', 'adm_small_icon',
'adm_large_icon', 'chrome_web_icon', 'chrome_web_image', 'firefox_icon', 'chrome_icon', 'ios_sound',
'android_sound', 'android_led_color', 'android_accent_color', 'android_visibility', 'adm_sound', 'ios_badgeType',
'ios_badgeCount', 'collapse_id', 'send_after', 'delayed_option', 'delivery_time_of_day', 'ttl', 'priority',
'android_group', 'android_group_message', 'adm_group', 'adm_group_message', 'isIos', 'isAndroid',
'isAnyWeb', 'isChromeWeb', 'isFirefox', 'isSafari', 'isWP', 'isWP_WNS', 'isAdm', 'isChrome',
'android_channel_id', 'existing_android_channel_id'];
/**
*
* @param initialBody The body must include either one of these: contents, content_available, template_id
......@@ -23,22 +10,7 @@ var Notification = function (initialBody) {
throw 'Body must be a JSON object';
}
this.postBody = {};
if ('contents' in initialBody) {
this.postBody.contents = initialBody.contents;
return;
}
if ('content_available' in initialBody) {
this.postBody.content_available = initialBody.content_available;
return;
}
if ('template_id' in initialBody) {
this.postBody.template_id = initialBody.template_id;
return;
}
throw 'Body must include one of the following fields: contents, content_available, template_id'
this.postBody = JSON.parse(JSON.stringify(initialBody));
};
/**
......@@ -46,11 +18,9 @@ var Notification = function (initialBody) {
* @param name { String }
* @param value
*/
// TODO Deprecate
Notification.prototype.setParameter = function (name, value) {
// TODO Deprecate
if (name && name[0] === '!') {
name = name.substring(1);
}
console.warn("[onesignal-node] setParameter function will be removed in next release, please check documentation.");
this.postBody[name] = value;
};
......@@ -58,7 +28,9 @@ Notification.prototype.setParameter = function (name, value) {
*
* @param contents
*/
// TODO Deprecate
Notification.prototype.setContent = function (contents) {
console.warn("[onesignal-node] setContent function will be removed in next release, please check documentation.");
this.postBody.contents = contents;
};
......@@ -66,7 +38,9 @@ Notification.prototype.setContent = function (contents) {
*
* @param included_segments The segment names you want to target
*/
// TODO Deprecate
Notification.prototype.setIncludedSegments = function (included_segments) {
console.warn("[onesignal-node] setIncludedSegments function will be removed in next release, please check documentation.");
this.postBody.included_segments = included_segments;
};
......@@ -74,7 +48,9 @@ Notification.prototype.setIncludedSegments = function (included_segments) {
*
* @param excluded_segments Segment that will be excluded when sending
*/
// TODO Deprecate
Notification.prototype.setExcludedSegments = function (excluded_segments) {
console.warn("[onesignal-node] setExcludedSegments function will be removed in next release, please check documentation.");
this.postBody.excluded_segments = excluded_segments;
};
......@@ -82,7 +58,9 @@ Notification.prototype.setExcludedSegments = function (excluded_segments) {
*
* @param filters
*/
// TODO Deprecate
Notification.prototype.setFilters = function (filters) {
console.warn("[onesignal-node] setFilters function will be removed in next release, please check documentation.");
this.postBody.filters = filters;
};
......@@ -90,7 +68,9 @@ Notification.prototype.setFilters = function (filters) {
*
* @param include_player_ids Specific players to send your notification to
*/
// TODO Deprecate
Notification.prototype.setTargetDevices = function (include_player_ids) {
console.warn("[onesignal-node] setTargetDevices function will be removed in next release, please check documentation.");
this.postBody.include_player_ids = include_player_ids;
};
......
This diff is collapsed.
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