pushy 2.0.10 → 2.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.DS_Store ADDED
Binary file
package/README.md CHANGED
@@ -5,7 +5,7 @@ The official Node.js package for sending push notifications with [Pushy](https:/
5
5
 
6
6
  > [Pushy](https://pushy.me/) is the most reliable push notification gateway, perfect for real-time, mission-critical applications.
7
7
 
8
- **Note:** If you don't have an existing Node.js project, consider using our [sample Node.js API project](https://github.com/pushy-me/pushy-node-backend) as a starting point to make things easier for you.
8
+ **Note:** If you don't have an existing Node.js project, consider using our [Node.js backend API sample project](https://github.com/pushy/pushy-node-backend) as a starting point to make things easier for you.
9
9
 
10
10
  ## Usage
11
11
 
@@ -15,13 +15,13 @@ First, install the package using npm:
15
15
  npm install pushy --save
16
16
  ```
17
17
 
18
- Then, use the following sample code to send a push notification to target devices:
18
+ Then, use the following code to send a push notification to target devices using the [Send Notifications API](https://pushy.me/docs/api/send-notifications):
19
19
 
20
20
  ```js
21
21
  var Pushy = require('pushy');
22
22
 
23
23
  // Plug in your Secret API Key
24
- // Get it here: https://dashboard.pushy.me/
24
+ // Get it from the Pushy Dashboard: https://dashboard.pushy.me/apps
25
25
  var pushy = new Pushy('SECRET_API_KEY');
26
26
 
27
27
  // Set push payload data to deliver to device(s)
@@ -29,8 +29,8 @@ var data = {
29
29
  message: 'Hello World!'
30
30
  };
31
31
 
32
- // Insert target device token(s) here
33
- var tokens = ['DEVICE_TOKEN'];
32
+ // Insert target device token(s), or set to Pub/Sub topic
33
+ var to = ['DEVICE_TOKEN'];
34
34
 
35
35
  // Set optional push notification options (such as iOS notification fields)
36
36
  var options = {
@@ -41,12 +41,11 @@ var options = {
41
41
  },
42
42
  };
43
43
 
44
- // Send push notification via the Send Notifications API
45
- // https://pushy.me/docs/api/send-notifications
46
- pushy.sendPushNotification(data, tokens, options, function (err, id) {
44
+ // Send push notification using the Send Notifications API
45
+ pushy.sendPushNotification(data, to, options, function (err, id) {
47
46
  // Log errors to console
48
47
  if (err) {
49
- return console.log('Fatal Error', err);
48
+ return console.error(err);
50
49
  }
51
50
 
52
51
  // Log success
@@ -54,7 +53,11 @@ pushy.sendPushNotification(data, tokens, options, function (err, id) {
54
53
  });
55
54
  ```
56
55
 
57
- Alternatively, send the notification using promises:
56
+ **Note:** Make sure to replace `SECRET_API_KEY` with your app's Secret API Key, available in the [Pushy Dashboard](https://dashboard.pushy.me/apps) (Click your app -> API Authentication tab).
57
+
58
+ ---
59
+
60
+ The library also supports using promise syntax instead of callbacks for all API methods:
58
61
 
59
62
  ```js
60
63
  pushy.sendPushNotification(data, tokens, options)
@@ -63,29 +66,160 @@ pushy.sendPushNotification(data, tokens, options)
63
66
  console.log('Push sent successfully! (ID: ' + id + ')');
64
67
  }).catch(function (err) {
65
68
  // Log errors to console
66
- return console.log(err);
69
+ return console.error(err);
67
70
  });
68
71
  ```
69
72
 
70
- Make sure to replace `SECRET_API_KEY` with your app's Secret API Key listed in the [Dashboard](https://dashboard.pushy.me/).
71
-
72
73
  ---
73
74
 
74
- To delete a notification (using the [Notification Deletion API](https://pushy.me/docs/api/notification-deletion)):
75
+ # Push APIs
76
+
77
+ ## pushy.sendPushNotification(data, to, options)
78
+
79
+ Instantly send push notifications to your users using the [Send Notifications API](https://pushy.me/docs/api/send-notifications) (see example above):
80
+
81
+ ```js
82
+ pushy.sendPushNotification(data, to, options, function (err, id) {
83
+ // Log errors to console
84
+ if (err) {
85
+ return console.error(err);
86
+ }
87
+
88
+ // Log success
89
+ console.log('Push sent successfully! (ID: ' + id + ')');
90
+ });
91
+ ```
92
+
93
+ ## pushy.getNotificationStatus(pushId)
94
+
95
+ Check the delivery status of your push notifications using the [Notification Status API](https://pushy.me/docs/api/notification-status):
75
96
 
76
97
  ```js
77
- // Enter unique push ID returned from pushy.sendPushNotification()
78
- var pushId = '5ea9b214b47cad768a35f13a';
98
+ pushy.getNotificationStatus('PUSH_ID', function (err, status) {
99
+ // Log errors to console
100
+ if (err) {
101
+ return console.error(err);
102
+ }
79
103
 
80
- // Delete the notification
81
- pushy.deletePushNotification(pushId)
82
- .then(function (id) {
83
- // Log success
84
- console.log('Push deleted successfully!');
85
- }).catch(function (err) {
86
- // Log errors to console
87
- return console.log(err);
88
- });
104
+ // Log notification status
105
+ console.log('Notification Status: ', JSON.stringify(status, null, 2));
106
+ });
107
+ ```
108
+
109
+ ## pushy.deletePushNotification(pushId)
110
+
111
+ Permanently delete a pending notification using the [Notification Deletion API](https://pushy.me/docs/api/notification-deletion):
112
+
113
+ ```js
114
+ pushy.deletePushNotification('PUSH_ID', function (err) {
115
+ // Log errors to console
116
+ if (err) {
117
+ return console.error(err);
118
+ }
119
+
120
+ // Log success
121
+ console.log('Pending notification deleted successfully');
122
+ });
123
+ ```
124
+
125
+ # Device APIs
126
+
127
+ ## pushy.getDeviceInfo(deviceToken)
128
+
129
+ Fetch device info, presence, undelivered notifications, and more by device token using the [Device Info API](https://pushy.me/docs/api/device):
130
+
131
+ ```js
132
+ pushy.getDeviceInfo('DEVICE_TOKEN', function (err, deviceInfo) {
133
+ // Log errors to console
134
+ if (err) {
135
+ return console.error(err);
136
+ }
137
+
138
+ // Log device info
139
+ console.log('Device Info: ', JSON.stringify(deviceInfo, null, 2));
140
+ });
141
+ ```
142
+
143
+ ## pushy.getDevicePresence(deviceTokens)
144
+
145
+ Check the presence and connectivity status of multiple devices using the [Device Presence API](https://pushy.me/docs/api/device-presence):
146
+
147
+ ```js
148
+ pushy.getDevicePresence(['DEVICE_TOKEN', 'DEVICE_TOKEN_2'], function (err, devicePresence) {
149
+ // Log errors to console
150
+ if (err) {
151
+ return console.error(err);
152
+ }
153
+
154
+ // Log device presence array
155
+ console.log('Device Presence: ', JSON.stringify(devicePresence, null, 2));
156
+ });
157
+ ```
158
+
159
+ # Pub/Sub APIs
160
+
161
+ ## pushy.getTopics()
162
+
163
+ Retrieve a list of your app's topics and subscribers count using the [Pub/Sub Topics API](https://pushy.me/docs/api/pubsub-topics):
164
+
165
+ ```js
166
+ pushy.getTopics(function (err, topics) {
167
+ // Log errors to console
168
+ if (err) {
169
+ return console.error(err);
170
+ }
171
+
172
+ // Log subscribed topics
173
+ console.log('Subscribed topics: \n' + JSON.stringify(topics, null, 2));
174
+ });
175
+ ```
176
+
177
+ ## pushy.getSubscribers(topic)
178
+
179
+ Retrieve a list of devices subscribed to a certain topic using the [Pub/Sub Subscribers API](https://pushy.me/docs/api/pubsub-subscribers):
180
+
181
+ ```js
182
+ pushy.getSubscribers('news', function (err, subscribers) {
183
+ // Log errors to console
184
+ if (err) {
185
+ return console.error(err);
186
+ }
187
+
188
+ // Log subscribed devices
189
+ console.log('Devices subscribed to topic: \n' + JSON.stringify(subscribers, null, 2));
190
+ });
191
+ ```
192
+
193
+ ## pushy.subscribe(topics, deviceToken)
194
+
195
+ Subscribe a device to one or more topics using the [Pub/Sub Subscribe API](https://pushy.me/docs/api/pubsub-subscribe):
196
+
197
+ ```js
198
+ pushy.subscribe(['news', 'weather'], 'DEVICE_TOKEN', function (err) {
199
+ // Log errors to console
200
+ if (err) {
201
+ return console.error(err);
202
+ }
203
+
204
+ // Log success
205
+ console.log('Subscribed device to topic(s) successfully');
206
+ });
207
+ ```
208
+
209
+ ## pushy.unsubscribe(topics, deviceToken)
210
+
211
+ Unsubscribe a device from one or more topics using the [Pub/Sub Unsubscribe API](https://pushy.me/docs/api/pubsub-unsubscribe)
212
+
213
+ ```js
214
+ pushy.unsubscribe(['news', 'weather'], 'DEVICE_TOKEN', function (err) {
215
+ // Log errors to console
216
+ if (err) {
217
+ return console.error(err);
218
+ }
219
+
220
+ // Log success
221
+ console.log('Unsubscribed device from topic(s) successfully');
222
+ });
89
223
  ```
90
224
 
91
225
  ## License
package/api/.DS_Store ADDED
Binary file
@@ -0,0 +1,64 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird');
3
+
4
+ // Device Info API
5
+ module.exports = function (deviceToken, callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Always return a promise
10
+ return new Promise(function (resolve, reject) {
11
+ // Custom callback provided?
12
+ if (callback) {
13
+ resolve = callback;
14
+ reject = callback;
15
+ }
16
+
17
+ // Device token passed in must be a string
18
+ if (typeof deviceToken !== 'string') {
19
+ return reject(new Error('Please provide the device token as a string.'));
20
+ }
21
+
22
+ // Make a request to the Device Info API
23
+ request(
24
+ Object.assign({
25
+ uri: that.getApiEndpoint() + '/devices/' + deviceToken + '?api_key=' + that.apiKey,
26
+ method: 'GET',
27
+ json: true
28
+ }, that.extraRequestOptions || {}), function (err, res, body) {
29
+ // Request error?
30
+ if (err) {
31
+ // Send to callback
32
+ return reject(err);
33
+ }
34
+
35
+ // Missing body?
36
+ if (!body) {
37
+ return reject(new Error('An empty body was received from the Pushy API.'));
38
+ }
39
+
40
+ // Pushy error?
41
+ if (body.error) {
42
+ return reject(new Error(body.error));
43
+ }
44
+
45
+ // Check for 200 OK
46
+ if (res.statusCode != 200) {
47
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
48
+ }
49
+
50
+ // Fetch result
51
+ var deviceInfo = body;
52
+
53
+ // Callback?
54
+ if (callback) {
55
+ // Invoke callback with device info result
56
+ callback(null, deviceInfo);
57
+ }
58
+ else {
59
+ // Resolve the promise
60
+ resolve(deviceInfo);
61
+ }
62
+ });
63
+ });
64
+ }
@@ -0,0 +1,84 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird');
3
+
4
+ // Device Presence API
5
+ module.exports = function (deviceTokens, callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Always return a promise
10
+ return new Promise(function (resolve, reject) {
11
+ // Custom callback provided?
12
+ if (callback) {
13
+ resolve = callback;
14
+ reject = callback;
15
+ }
16
+
17
+ // Topics passed in must be in string or array format
18
+ if (!Array.isArray(deviceTokens)) {
19
+ // In case a single token was passed in, convert to array
20
+ if (typeof deviceTokens === 'string') {
21
+ deviceTokens = [deviceTokens];
22
+ }
23
+ else {
24
+ // Throw error for all other input types
25
+ return reject(new Error('Please provide the device token parameter as an array of strings.'));
26
+ }
27
+ }
28
+
29
+ // Validate every device token to be a string
30
+ for (var deviceToken of deviceTokens) {
31
+ if (typeof deviceToken !== 'string') {
32
+ return reject(new Error('Please ensure all device tokens passed in are strings.'));
33
+ }
34
+ }
35
+
36
+ // Prepare JSON post data
37
+ var postData = {};
38
+
39
+ // Add devices tokens to the post body
40
+ postData.tokens = deviceTokens;
41
+
42
+ // Make a request to the Device Presence API
43
+ request(
44
+ Object.assign({
45
+ uri: that.getApiEndpoint() + '/devices/presence' + '?api_key=' + that.apiKey,
46
+ method: 'POST',
47
+ json: postData
48
+ }, that.extraRequestOptions || {}), function (err, res, body) {
49
+ // Request error?
50
+ if (err) {
51
+ // Send to callback
52
+ return reject(err);
53
+ }
54
+
55
+ // Missing body?
56
+ if (!body) {
57
+ return reject(new Error('An empty body was received from the Pushy API.'));
58
+ }
59
+
60
+ // Pushy error?
61
+ if (body.error) {
62
+ return reject(new Error(body.error));
63
+ }
64
+
65
+ // Check for 200 OK
66
+ if (res.statusCode != 200) {
67
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
68
+ }
69
+
70
+ // Fetch result
71
+ var devicePresence = body.presence;
72
+
73
+ // Callback?
74
+ if (callback) {
75
+ // Invoke callback with device presence result
76
+ callback(null, devicePresence);
77
+ }
78
+ else {
79
+ // Resolve the promise
80
+ resolve(devicePresence);
81
+ }
82
+ });
83
+ });
84
+ }
@@ -0,0 +1,75 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird');
3
+
4
+ // Pub/Sub Subscribe API
5
+ module.exports = function (topics, deviceToken, callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Always return a promise
10
+ return new Promise(function (resolve, reject) {
11
+ // Custom callback provided?
12
+ if (callback) {
13
+ resolve = callback;
14
+ reject = callback;
15
+ }
16
+
17
+ // Device token passed in must be a string
18
+ if (typeof deviceToken !== 'string') {
19
+ return reject(new Error('Please provide the device token as a string.'));
20
+ }
21
+
22
+ // Topics passed in must be in string or array format
23
+ if (typeof topics !== 'string' && !Array.isArray(topics)) {
24
+ return reject(new Error('Please provide the Pub/Sub topics parameter as a string or an array of strings.'));
25
+ }
26
+
27
+ // Prepare JSON post data
28
+ var postData = {};
29
+
30
+ // Add token to the post body
31
+ postData.token = deviceToken;
32
+
33
+ // Convert singular string topic to array
34
+ postData.topics = Array.isArray(topics) ? topics : [topics];
35
+
36
+ // Make a request to the Pub/Sub Subscribe API
37
+ request(
38
+ Object.assign({
39
+ uri: that.getApiEndpoint() + '/topics/subscribe/' + '?api_key=' + that.apiKey,
40
+ method: 'POST',
41
+ json: postData
42
+ }, that.extraRequestOptions || {}), function (err, res, body) {
43
+ // Request error?
44
+ if (err) {
45
+ // Send to callback
46
+ return reject(err);
47
+ }
48
+
49
+ // Missing body?
50
+ if (!body) {
51
+ return reject(new Error('An empty body was received from the Pushy API.'));
52
+ }
53
+
54
+ // Pushy error?
55
+ if (body.error) {
56
+ return reject(new Error(body.error));
57
+ }
58
+
59
+ // Check for 200 OK
60
+ if (res.statusCode != 200) {
61
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
62
+ }
63
+
64
+ // Callback?
65
+ if (callback) {
66
+ // Pass null error (success)
67
+ callback(null);
68
+ }
69
+ else {
70
+ // Resolve the promise successfully
71
+ resolve();
72
+ }
73
+ });
74
+ });
75
+ }
@@ -0,0 +1,64 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird');
3
+
4
+ // Pub/Sub Subscribers API
5
+ module.exports = function (topic, callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Always return a promise
10
+ return new Promise(function (resolve, reject) {
11
+ // Custom callback provided?
12
+ if (callback) {
13
+ resolve = callback;
14
+ reject = callback;
15
+ }
16
+
17
+ // Check the validity of topic
18
+ if (!topic || typeof topic !== 'string') {
19
+ return reject(new Error('Invalid topic name'));
20
+ }
21
+
22
+ // Make a request to the Pub/Sub Subscribers API
23
+ request(
24
+ Object.assign({
25
+ uri: that.getApiEndpoint() + '/topics/' + topic + '?api_key=' + that.apiKey,
26
+ method: 'GET',
27
+ json: true
28
+ }, that.extraRequestOptions || {}), function (err, res, body) {
29
+ // Request error?
30
+ if (err) {
31
+ // Send to callback
32
+ return reject(err);
33
+ }
34
+
35
+ // Missing body?
36
+ if (!body) {
37
+ return reject(new Error('An empty body was received from the Pushy API.'));
38
+ }
39
+
40
+ // Pushy error?
41
+ if (body.error) {
42
+ return reject(new Error(body.error));
43
+ }
44
+
45
+ // Check for 200 OK
46
+ if (res.statusCode != 200) {
47
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
48
+ }
49
+
50
+ // Fetch result
51
+ var subscribers = body.subscribers;
52
+
53
+ // Callback?
54
+ if (callback) {
55
+ // Invoke callback with subscribers list
56
+ callback(null, subscribers);
57
+ }
58
+ else {
59
+ // Resolve the promise
60
+ resolve(body);
61
+ }
62
+ });
63
+ });
64
+ }
@@ -0,0 +1,59 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird');
3
+
4
+ // Pub/Sub Topics API
5
+ module.exports = function (callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Always return a promise
10
+ return new Promise(function (resolve, reject) {
11
+ // Custom callback provided?
12
+ if (callback) {
13
+ resolve = callback;
14
+ reject = callback;
15
+ }
16
+
17
+ // Make a request to the Pub/Sub Topics API
18
+ request(
19
+ Object.assign({
20
+ uri: that.getApiEndpoint() + '/topics/' + '?api_key=' + that.apiKey,
21
+ method: 'GET',
22
+ json: true
23
+ }, that.extraRequestOptions || {}), function (err, res, body) {
24
+ // Request error?
25
+ if (err) {
26
+ // Send to callback
27
+ return reject(err);
28
+ }
29
+
30
+ // Missing body?
31
+ if (!body) {
32
+ return reject(new Error('An empty body was received from the Pushy API.'));
33
+ }
34
+
35
+ // Pushy error?
36
+ if (body.error) {
37
+ return reject(new Error(body.error));
38
+ }
39
+
40
+ // Check for 200 OK
41
+ if (res.statusCode != 200) {
42
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
43
+ }
44
+
45
+ // Fetch result
46
+ var topics = body.topics;
47
+
48
+ // Callback?
49
+ if (callback) {
50
+ // Invoke callback with topics list
51
+ callback(null, topics);
52
+ }
53
+ else {
54
+ // Resolve the promise
55
+ resolve(body);
56
+ }
57
+ });
58
+ });
59
+ }
@@ -0,0 +1,75 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird');
3
+
4
+ // Pub/Sub Unsubscribe API
5
+ module.exports = function (topics, deviceToken, callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Always return a promise
10
+ return new Promise(function (resolve, reject) {
11
+ // Custom callback provided?
12
+ if (callback) {
13
+ resolve = callback;
14
+ reject = callback;
15
+ }
16
+
17
+ // Device token passed in must be a string
18
+ if (typeof deviceToken !== 'string') {
19
+ return reject(new Error('Please provide the device token as a string.'));
20
+ }
21
+
22
+ // Topics passed in must be in string or array format
23
+ if (typeof topics !== 'string' && !Array.isArray(topics)) {
24
+ return reject(new Error('Please provide the Pub/Sub topics parameter as a string or an array of strings.'));
25
+ }
26
+
27
+ // Prepare JSON post data
28
+ var postData = {};
29
+
30
+ // Add token to the post body
31
+ postData.token = deviceToken;
32
+
33
+ // Convert singular string topic to array
34
+ postData.topics = Array.isArray(topics) ? topics : [topics];
35
+
36
+ // Make a request to the Pub/Sub Unsubscribe API
37
+ request(
38
+ Object.assign({
39
+ uri: that.getApiEndpoint() + '/topics/unsubscribe/' + '?api_key=' + that.apiKey,
40
+ method: 'POST',
41
+ json: postData
42
+ }, that.extraRequestOptions || {}), function (err, res, body) {
43
+ // Request error?
44
+ if (err) {
45
+ // Send to callback
46
+ return reject(err);
47
+ }
48
+
49
+ // Missing body?
50
+ if (!body) {
51
+ return reject(new Error('An empty body was received from the Pushy API.'));
52
+ }
53
+
54
+ // Pushy error?
55
+ if (body.error) {
56
+ return reject(new Error(body.error));
57
+ }
58
+
59
+ // Check for 200 OK
60
+ if (res.statusCode != 200) {
61
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
62
+ }
63
+
64
+ // Callback?
65
+ if (callback) {
66
+ // Pass null error (success)
67
+ callback(null);
68
+ }
69
+ else {
70
+ // Resolve the promise successfully
71
+ resolve();
72
+ }
73
+ });
74
+ });
75
+ }
@@ -0,0 +1,71 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird')
3
+
4
+ // Notification Deletion API
5
+ module.exports = function (pushId, callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Always return a promise
10
+ return new Promise(function (resolve, reject) {
11
+ // Custom callback provided?
12
+ if (callback) {
13
+ resolve = callback;
14
+ reject = callback;
15
+ }
16
+
17
+ // No pushId provided?
18
+ if (!pushId) {
19
+ return reject(new Error('Please provide the notification ID you wish to delete.'));
20
+ }
21
+
22
+ // pushId must be an string
23
+ if (typeof pushId !== 'string') {
24
+ return reject(new Error('Please provide the notification ID as a string.'));
25
+ }
26
+
27
+ // Callback must be a function (if provided)
28
+ if (callback && typeof callback !== 'function') {
29
+ return reject(new Error('Please provide the callback parameter as a function.'));
30
+ }
31
+
32
+ // Make a request to the Notification Deletion API
33
+ request(
34
+ Object.assign({
35
+ uri: that.getApiEndpoint() + '/pushes/' + pushId + '?api_key=' + that.apiKey,
36
+ method: 'DELETE',
37
+ json: true
38
+ }, that.extraRequestOptions || {}), function (err, res, body) {
39
+ // Request error?
40
+ if (err) {
41
+ // Send to callback
42
+ return reject(err);
43
+ }
44
+
45
+ // Missing body?
46
+ if (!body) {
47
+ return reject(new Error('An empty body was received from the Pushy API.'));
48
+ }
49
+
50
+ // Pushy error?
51
+ if (body.error) {
52
+ return reject(new Error(body.error));
53
+ }
54
+
55
+ // Check for 200 OK
56
+ if (res.statusCode != 200) {
57
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
58
+ }
59
+
60
+ // Callback?
61
+ if (callback) {
62
+ // Invoke callback with a null error
63
+ callback(null);
64
+ }
65
+ else {
66
+ // Resolve the promise
67
+ resolve();
68
+ }
69
+ });
70
+ });
71
+ };
@@ -0,0 +1,103 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird');
3
+
4
+ // Send Notifications API
5
+ module.exports = function (data, recipient, options, callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Support empty options
10
+ options = options || {};
11
+
12
+ // Always return a promise
13
+ return new Promise(function (resolve, reject) {
14
+ // Custom callback provided?
15
+ if (callback) {
16
+ resolve = callback;
17
+ reject = callback;
18
+ }
19
+
20
+ // No data provided?
21
+ if (!data) {
22
+ return reject(new Error('Please provide the push payload to send to devices.'));
23
+ }
24
+
25
+ // Data must be an object
26
+ if ((Object.prototype.toString.call(data) !== '[object Object]')) {
27
+ return reject(new Error('Please provide the push payload as an object.'));
28
+ }
29
+
30
+ // No recipient provided?
31
+ if (!recipient) {
32
+ return reject(new Error('Please provide the notification recipient.'));
33
+ }
34
+
35
+ // Options must be an object
36
+ if (Object.prototype.toString.call(options) !== '[object Object]') {
37
+ return reject(new Error('Please provide the options parameter as an object.'));
38
+ }
39
+
40
+ // Prepare JSON post data (defaults to options object)
41
+ var postData = options;
42
+
43
+ // Set payload and device tokens
44
+ postData.data = data;
45
+
46
+ // Recipient provided as string?
47
+ if (typeof recipient === 'string' || Array.isArray(recipient)) {
48
+ // Set "to" parameter
49
+ postData.to = recipient;
50
+ }
51
+ else {
52
+ // Invalid recipient type
53
+ return reject(new Error('Please provide the notification recipient as a string or an array of strings.'));
54
+ }
55
+
56
+ // Callback must be a function (if provided)
57
+ if (callback && typeof callback !== 'function') {
58
+ return reject(new Error('Please provide the callback parameter as a function.'));
59
+ }
60
+
61
+ // Make a request to the Send Notifications API
62
+ request(
63
+ Object.assign({
64
+ uri: that.getApiEndpoint() + '/push?api_key=' + that.apiKey,
65
+ method: 'POST',
66
+ json: postData
67
+ }, that.extraRequestOptions || {}), function (err, res, body) {
68
+ // Request error?
69
+ if (err) {
70
+ // Send to callback
71
+ return reject(err);
72
+ }
73
+
74
+ // Missing body?
75
+ if (!body) {
76
+ return reject(new Error('An empty body was received from the Pushy API.'));
77
+ }
78
+
79
+ // Pushy error?
80
+ if (body.error) {
81
+ return reject(new Error(body.error));
82
+ }
83
+
84
+ // Check for 200 OK
85
+ if (res.statusCode != 200) {
86
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
87
+ }
88
+
89
+ // Fetch push notification ID
90
+ var pushId = body.id;
91
+
92
+ // Callback?
93
+ if (callback) {
94
+ // Pass push ID to callback with a null error
95
+ callback(null, pushId);
96
+ }
97
+ else {
98
+ // Resolve the promise
99
+ resolve(pushId);
100
+ }
101
+ });
102
+ });
103
+ };
@@ -0,0 +1,64 @@
1
+ var request = require('request');
2
+ var Promise = require('bluebird');
3
+
4
+ // Notification Status API
5
+ module.exports = function (pushId, callback) {
6
+ // Keep track of instance 'this'
7
+ var that = this;
8
+
9
+ // Always return a promise
10
+ return new Promise(function (resolve, reject) {
11
+ // Custom callback provided?
12
+ if (callback) {
13
+ resolve = callback;
14
+ reject = callback;
15
+ }
16
+
17
+ // Device token passed in must be a string
18
+ if (typeof pushId !== 'string') {
19
+ return reject(new Error('Please provide the device token as a string.'));
20
+ }
21
+
22
+ // Make a request to the Notification Status API
23
+ request(
24
+ Object.assign({
25
+ uri: that.getApiEndpoint() + '/pushes/' + pushId + '?api_key=' + that.apiKey,
26
+ method: 'GET',
27
+ json: true
28
+ }, that.extraRequestOptions || {}), function (err, res, body) {
29
+ // Request error?
30
+ if (err) {
31
+ // Send to callback
32
+ return reject(err);
33
+ }
34
+
35
+ // Missing body?
36
+ if (!body) {
37
+ return reject(new Error('An empty body was received from the Pushy API.'));
38
+ }
39
+
40
+ // Pushy error?
41
+ if (body.error) {
42
+ return reject(new Error(body.error));
43
+ }
44
+
45
+ // Check for 200 OK
46
+ if (res.statusCode != 200) {
47
+ return reject(new Error('An invalid response code was received from the Pushy API.'));
48
+ }
49
+
50
+ // Fetch result
51
+ var status = body.push;
52
+
53
+ // Callback?
54
+ if (callback) {
55
+ // Invoke callback with notification status
56
+ callback(null, status);
57
+ }
58
+ else {
59
+ // Resolve the promise
60
+ resolve(status);
61
+ }
62
+ });
63
+ });
64
+ }
@@ -0,0 +1,28 @@
1
+ // Change to require('pushy') to use this code in your own project
2
+ var Pushy = require('../');
3
+
4
+ // Plug in your Secret API Key
5
+ // Get it from the Pushy Dashboard: https://dashboard.pushy.me/apps
6
+ var pushy = new Pushy('SECRET_API_KEY');
7
+
8
+ // Fetch device info, presence, undelivered notifications, and more by device token
9
+ pushy.getDeviceInfo('DEVICE_TOKEN', function (err, deviceInfo) {
10
+ // Log errors to console
11
+ if (err) {
12
+ return console.error(err);
13
+ }
14
+
15
+ // Log device info
16
+ console.log('Device Info: ', JSON.stringify(deviceInfo, null, 2));
17
+ });
18
+
19
+ // Check the presence and connectivity status of multiple devices
20
+ pushy.getDevicePresence(['DEVICE_TOKEN', 'DEVICE_TOKEN_2'], function (err, devicePresence) {
21
+ // Log errors to console
22
+ if (err) {
23
+ return console.error(err);
24
+ }
25
+
26
+ // Log device presence array
27
+ console.log('Device Presence: ', JSON.stringify(devicePresence, null, 2));
28
+ });
@@ -0,0 +1,50 @@
1
+ // Change to require('pushy') to use this code in your own project
2
+ var Pushy = require('../');
3
+
4
+ // Plug in your Secret API Key
5
+ // Get it from the Pushy Dashboard: https://dashboard.pushy.me/apps
6
+ var pushy = new Pushy('SECRET_API_KEY');
7
+
8
+ // Retrieve a list of your app's topics and subscribers count using the Pub/Sub Topics API
9
+ pushy.getTopics(function (err, topics) {
10
+ // Log errors to console
11
+ if (err) {
12
+ return console.error(err);
13
+ }
14
+
15
+ // Log subscribed topics
16
+ console.log('Subscribed topics: \n' + JSON.stringify(topics, null, 2));
17
+ });
18
+
19
+ // Retrieve a list of devices subscribed to a certain topic using the Pub/Sub Subscribers API
20
+ pushy.getSubscribers('news', function (err, subscribers) {
21
+ // Log errors to console
22
+ if (err) {
23
+ return console.error(err);
24
+ }
25
+
26
+ // Log subscribed devices
27
+ console.log('Devices subscribed to topic: \n' + JSON.stringify(subscribers, null, 2));
28
+ });
29
+
30
+ // Subscribe a device to one or more topics using the Pub/Sub Subscribe API
31
+ pushy.subscribe(['news', 'weather'], 'DEVICE_TOKEN', function (err) {
32
+ // Log errors to console
33
+ if (err) {
34
+ return console.error(err);
35
+ }
36
+
37
+ // Log success
38
+ console.log('Subscribed device to topic(s) successfully');
39
+ });
40
+
41
+ // Unsubscribe a device from one or more topics using the Pub/Sub Unsubscribe API
42
+ pushy.unsubscribe(['news', 'weather'], 'DEVICE_TOKEN', function (err) {
43
+ // Log errors to console
44
+ if (err) {
45
+ return console.error(err);
46
+ }
47
+
48
+ // Log success
49
+ console.log('Unsubscribed device from topic(s) successfully');
50
+ });
@@ -0,0 +1,56 @@
1
+ // Change to require('pushy') to use this code in your own project
2
+ var Pushy = require('../');
3
+
4
+ // Plug in your Secret API Key
5
+ // Get it from the Pushy Dashboard: https://dashboard.pushy.me/apps
6
+ var pushy = new Pushy('SECRET_API_KEY');
7
+
8
+ // Set push payload data to deliver to device(s)
9
+ var data = {
10
+ message: 'Hello World!'
11
+ };
12
+
13
+ // Insert target device token(s), or set to Pub/Sub topic
14
+ var to = ['DEVICE_TOKEN'];
15
+
16
+ // Set optional push notification options (such as iOS notification fields)
17
+ var options = {
18
+ notification: {
19
+ badge: 1,
20
+ sound: 'ping.aiff',
21
+ body: 'Hello World \u270c'
22
+ },
23
+ };
24
+
25
+ // Send push notification using the Send Notifications API
26
+ pushy.sendPushNotification(data, to, options, function (err, id) {
27
+ // Log errors to console
28
+ if (err) {
29
+ return console.error(err);
30
+ }
31
+
32
+ // Log success
33
+ console.log('Push sent successfully! (ID: ' + id + ')');
34
+ });
35
+
36
+ // Check the delivery status of your push notifications using the Notification Status API
37
+ pushy.getNotificationStatus('PUSH_ID', function (err, status) {
38
+ // Log errors to console
39
+ if (err) {
40
+ return console.error(err);
41
+ }
42
+
43
+ // Log notification status
44
+ console.log('Notification Status: ', JSON.stringify(status, null, 2));
45
+ });
46
+
47
+ // Permanently delete a pending notification using the Notification Deletion API
48
+ pushy.deletePushNotification('PUSH_ID', function (err) {
49
+ // Log errors to console
50
+ if (err) {
51
+ return console.error(err);
52
+ }
53
+
54
+ // Log success
55
+ console.log('Pending notification deleted successfully');
56
+ });
package/index.js CHANGED
@@ -1,14 +1,11 @@
1
- var request = require('request');
2
- var Promise = require('bluebird');
3
-
4
1
  // Pushy API endpoint
5
2
  var apiEndpoint = 'https://api.pushy.me';
6
3
 
7
4
  // Package constructor
8
5
  function Pushy(apiKey) {
9
- // Make sure the developer provided his/her API key
6
+ // Make sure the developer provided an API key
10
7
  if (!apiKey) {
11
- throw new Error('Please provide your API key to use this package.');
8
+ throw new Error('Please provide your Secret API key to use this package.');
12
9
  }
13
10
 
14
11
  // Check for alphanumeric API key
@@ -20,177 +17,35 @@ function Pushy(apiKey) {
20
17
  this.apiKey = apiKey;
21
18
  }
22
19
 
23
- // Send Notifications API
24
- Pushy.prototype.sendPushNotification = function (data, recipient, options, callback) {
25
- // Keep track of instance 'this'
26
- var that = this;
27
-
28
- // Support empty options
29
- options = options || {};
30
-
31
- // Always return a promise
32
- return new Promise(function (resolve, reject) {
33
- // Custom callback provided?
34
- if (callback) {
35
- resolve = callback;
36
- reject = callback;
37
- }
38
-
39
- // No data provided?
40
- if (!data) {
41
- return reject(new Error('Please provide the push payload to send to devices.'));
42
- }
43
-
44
- // Data must be an object
45
- if ((Object.prototype.toString.call(data) !== '[object Object]')) {
46
- return reject(new Error('Please provide the push payload as an object.'));
47
- }
48
-
49
- // No recipient provided?
50
- if (!recipient) {
51
- return reject(new Error('Please provide the notification recipient.'));
52
- }
53
-
54
- // Options must be an object
55
- if (Object.prototype.toString.call(options) !== '[object Object]') {
56
- return reject(new Error('Please provide the options parameter as an object.'));
57
- }
58
-
59
- // Prepare JSON post data (defaults to options object)
60
- var postData = options;
61
-
62
- // Set payload and device tokens
63
- postData.data = data;
64
-
65
- // Recipient provided as string?
66
- if (typeof recipient === 'string' || Array.isArray(recipient)) {
67
- // Set "to" parameter
68
- postData.to = recipient;
69
- }
70
- else {
71
- // Invalid recipient type
72
- return reject(new Error('Please provide the notification recipient as a string or an array of strings.'));
73
- }
74
-
75
- // Callback must be a function (if provided)
76
- if (callback && typeof callback !== 'function') {
77
- return reject(new Error('Please provide the callback parameter as a function.'));
78
- }
79
-
80
- // Send push using the "request" package
81
- request(Object.assign({
82
- uri: that.getApiEndpoint() + '/push?api_key=' + that.apiKey,
83
- method: 'POST',
84
- json: postData
85
- }, that.extraRequestOptions || {}), function (err, res, body) {
86
- // Request error?
87
- if (err) {
88
- // Send to callback
89
- return reject(err);
90
- }
91
-
92
- // Missing body?
93
- if (!body) {
94
- return reject(new Error('An empty body was received from the Pushy API.'));
95
- }
96
-
97
- // Pushy error?
98
- if (body.error) {
99
- return reject(new Error(body.error));
100
- }
101
-
102
- // Check for 200 OK
103
- if (res.statusCode != 200) {
104
- return reject(new Error('An invalid response code was received from the Pushy API.'));
105
- }
106
-
107
- // Fetch push notification ID
108
- var pushId = body.id;
20
+ // Push APIs
21
+ Pushy.prototype.sendPushNotification = require('./api/push/send');
22
+ Pushy.prototype.getNotificationStatus = require('./api/push/status');
23
+ Pushy.prototype.deletePushNotification = require('./api/push/delete');
109
24
 
110
- // Callback?
111
- if (callback) {
112
- // Pass push ID to callback with a null error
113
- callback(null, pushId);
114
- }
115
- else {
116
- // Resolve the promise
117
- resolve(pushId);
118
- }
119
- });
120
- });
121
- };
25
+ // Device APIs
26
+ Pushy.prototype.getDeviceInfo = require('./api/device/info');
27
+ Pushy.prototype.getDevicePresence = require('./api/device/presence');
122
28
 
123
- // Notification Deletion API
124
- Pushy.prototype.deletePushNotification = function (pushId, callback) {
125
- // Keep track of instance 'this'
126
- var that = this;
29
+ // Pub/Sub APIs
30
+ Pushy.prototype.getTopics = require('./api/pubsub/topics');
31
+ Pushy.prototype.subscribe = require('./api/pubsub/subscribe');
32
+ Pushy.prototype.unsubscribe = require('./api/pubsub/unsubscribe');
33
+ Pushy.prototype.getSubscribers = require('./api/pubsub/subscribers');
127
34
 
128
- // Always return a promise
129
- return new Promise(function (resolve, reject) {
130
- // Custom callback provided?
131
- if (callback) {
132
- resolve = callback;
133
- reject = callback;
134
- }
135
-
136
- // No pushId provided?
137
- if (!pushId) {
138
- return reject(new Error('Please provide the notification ID you wish to delete.'));
139
- }
140
-
141
- // pushId must be an string
142
- if (typeof pushId !== 'string') {
143
- return reject(new Error('Please provide the notification ID as a string.'));
144
- }
145
-
146
- // Callback must be a function (if provided)
147
- if (callback && typeof callback !== 'function') {
148
- return reject(new Error('Please provide the callback parameter as a function.'));
149
- }
150
-
151
- // Delete push using the "request" package
152
- request(Object.assign({
153
- uri: that.getApiEndpoint() + '/pushes/' + pushId + '?api_key=' + that.apiKey,
154
- method: 'DELETE',
155
- }, that.extraRequestOptions || {}), function (err, res, body) {
156
- // Request error?
157
- if (err) {
158
- // Send to callback
159
- return reject(err);
160
- }
161
-
162
- // Check for 200 OK
163
- if (res.statusCode != 200) {
164
- return reject(new Error('An invalid response code was received from the Pushy API.'));
165
- }
166
-
167
- // Callback?
168
- if (callback) {
169
- // Invoke callback with a null error
170
- callback(null);
171
- }
172
- else {
173
- // Resolve the promise
174
- resolve();
175
- }
176
- });
177
- });
178
- };
35
+ // API endpoint selector
36
+ Pushy.prototype.getApiEndpoint = function () {
37
+ return (this.enterpriseEndpoint) ? this.enterpriseEndpoint : apiEndpoint;
38
+ }
179
39
 
180
40
  // Support for Pushy Enterprise
181
41
  Pushy.prototype.setEnterpriseConfig = function (endpoint) {
182
42
  this.enterpriseEndpoint = endpoint;
183
43
  }
184
44
 
185
- // API endpoint selector
186
- Pushy.prototype.getApiEndpoint = function () {
187
- return (this.enterpriseEndpoint) ? this.enterpriseEndpoint : apiEndpoint;
188
- }
189
-
190
45
  // Add extra options that will be passed to the request library
191
46
  Pushy.prototype.setExtraRequestOptions = function (extraRequestOptions) {
192
47
  this.extraRequestOptions = extraRequestOptions;
193
48
  }
194
49
 
195
50
  // Expose the Pushy object
196
- module.exports = Pushy;
51
+ module.exports = Pushy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pushy",
3
- "version": "2.0.10",
3
+ "version": "2.0.13",
4
4
  "description": "The official Node.js package for sending push notifications with Pushy.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -8,14 +8,14 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/pushy-me/pushy-node.git"
11
+ "url": "git+https://github.com/pushy/pushy-node.git"
12
12
  },
13
13
  "author": "Pushy <support@pushy.me>",
14
14
  "license": "Apache-2.0",
15
15
  "bugs": {
16
- "url": "https://github.com/pushy-me/pushy-node/issues"
16
+ "url": "https://github.com/pushy/pushy-node/issues"
17
17
  },
18
- "homepage": "https://github.com/pushy-me/pushy-node#readme",
18
+ "homepage": "https://github.com/pushy/pushy-node#readme",
19
19
  "dependencies": {
20
20
  "bluebird": "^3.4.1",
21
21
  "request": "^2.72.0"
package/examples/send.js DELETED
@@ -1,35 +0,0 @@
1
- // Change to require('pushy') to use this code in your own project
2
- var Pushy = require('../');
3
-
4
- // Plug in your Secret API Key
5
- // Get it here: https://dashboard.pushy.me/
6
- var pushy = new Pushy('SECRET_API_KEY');
7
-
8
- // Set push payload data to deliver to device(s)
9
- var data = {
10
- message: 'Hello World!'
11
- };
12
-
13
- // Insert target device token(s) here
14
- var tokens = ['DEVICE_TOKEN'];
15
-
16
- // Set optional push notification options (such as iOS notification fields)
17
- var options = {
18
- notification: {
19
- badge: 1,
20
- sound: 'ping.aiff',
21
- body: 'Hello World \u270c'
22
- },
23
- };
24
-
25
- // Send push notification via the Send Notifications API
26
- // https://pushy.me/docs/api/send-notifications
27
- pushy.sendPushNotification(data, tokens, options, function (err, id) {
28
- // Log errors to console
29
- if (err) {
30
- return console.log('Fatal Error', err);
31
- }
32
-
33
- // Log success
34
- console.log('Push sent successfully! (ID: ' + id + ')');
35
- });