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 # 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' }
......
'use strict'; '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 * @param initialBody The body must include either one of these: contents, content_available, template_id
...@@ -23,22 +10,7 @@ var Notification = function (initialBody) { ...@@ -23,22 +10,7 @@ var Notification = function (initialBody) {
throw 'Body must be a JSON object'; throw 'Body must be a JSON object';
} }
this.postBody = {}; this.postBody = JSON.parse(JSON.stringify(initialBody));
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'
}; };
/** /**
...@@ -46,11 +18,9 @@ var Notification = function (initialBody) { ...@@ -46,11 +18,9 @@ var Notification = function (initialBody) {
* @param name { String } * @param name { String }
* @param value * @param value
*/ */
// TODO Deprecate
Notification.prototype.setParameter = function (name, value) { Notification.prototype.setParameter = function (name, value) {
// TODO Deprecate console.warn("[onesignal-node] setParameter function will be removed in next release, please check documentation.");
if (name && name[0] === '!') {
name = name.substring(1);
}
this.postBody[name] = value; this.postBody[name] = value;
}; };
...@@ -58,7 +28,9 @@ Notification.prototype.setParameter = function (name, value) { ...@@ -58,7 +28,9 @@ Notification.prototype.setParameter = function (name, value) {
* *
* @param contents * @param contents
*/ */
// TODO Deprecate
Notification.prototype.setContent = function (contents) { Notification.prototype.setContent = function (contents) {
console.warn("[onesignal-node] setContent function will be removed in next release, please check documentation.");
this.postBody.contents = contents; this.postBody.contents = contents;
}; };
...@@ -66,7 +38,9 @@ Notification.prototype.setContent = function (contents) { ...@@ -66,7 +38,9 @@ Notification.prototype.setContent = function (contents) {
* *
* @param included_segments The segment names you want to target * @param included_segments The segment names you want to target
*/ */
// TODO Deprecate
Notification.prototype.setIncludedSegments = function (included_segments) { 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; this.postBody.included_segments = included_segments;
}; };
...@@ -74,7 +48,9 @@ Notification.prototype.setIncludedSegments = function (included_segments) { ...@@ -74,7 +48,9 @@ Notification.prototype.setIncludedSegments = function (included_segments) {
* *
* @param excluded_segments Segment that will be excluded when sending * @param excluded_segments Segment that will be excluded when sending
*/ */
// TODO Deprecate
Notification.prototype.setExcludedSegments = function (excluded_segments) { 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; this.postBody.excluded_segments = excluded_segments;
}; };
...@@ -82,7 +58,9 @@ Notification.prototype.setExcludedSegments = function (excluded_segments) { ...@@ -82,7 +58,9 @@ Notification.prototype.setExcludedSegments = function (excluded_segments) {
* *
* @param filters * @param filters
*/ */
// TODO Deprecate
Notification.prototype.setFilters = function (filters) { Notification.prototype.setFilters = function (filters) {
console.warn("[onesignal-node] setFilters function will be removed in next release, please check documentation.");
this.postBody.filters = filters; this.postBody.filters = filters;
}; };
...@@ -90,7 +68,9 @@ Notification.prototype.setFilters = function (filters) { ...@@ -90,7 +68,9 @@ Notification.prototype.setFilters = function (filters) {
* *
* @param include_player_ids Specific players to send your notification to * @param include_player_ids Specific players to send your notification to
*/ */
// TODO Deprecate
Notification.prototype.setTargetDevices = function (include_player_ids) { 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.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