pushy 2.0.11 → 2.0.14

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 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,44 +66,107 @@ 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
- # Additional API Methods
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):
96
+
97
+ ```js
98
+ pushy.getNotificationStatus('PUSH_ID', function (err, status) {
99
+ // Log errors to console
100
+ if (err) {
101
+ return console.error(err);
102
+ }
103
+
104
+ // Log notification status
105
+ console.log('Notification Status: ', JSON.stringify(status, null, 2));
106
+ });
107
+ ```
75
108
 
76
109
  ## pushy.deletePushNotification(pushId)
77
110
 
78
- Delete a notification using the [Notification Deletion API](https://pushy.me/docs/api/notification-deletion):
111
+ Permanently delete a pending notification using the [Notification Deletion API](https://pushy.me/docs/api/notification-deletion):
79
112
 
80
113
  ```js
81
- // Unique push ID returned from pushy.sendPushNotification()
82
- var pushId = '5ea9b214b47cad768a35f13a';
114
+ pushy.deletePushNotification('PUSH_ID', function (err) {
115
+ // Log errors to console
116
+ if (err) {
117
+ return console.error(err);
118
+ }
83
119
 
84
- // Delete the notification
85
- pushy.deletePushNotification(pushId)
86
- .then(function (id) {
87
- // Log success
88
- console.log('Push deleted successfully!');
89
- }).catch(function (err) {
90
- // Log errors to console
91
- return console.log(err);
92
- });
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
+ });
93
157
  ```
94
158
 
159
+ # Pub/Sub APIs
160
+
95
161
  ## pushy.getTopics()
96
162
 
97
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):
98
164
 
99
165
  ```js
100
- pushy.getTopics((err, topics) => {
166
+ pushy.getTopics(function (err, topics) {
101
167
  // Log errors to console
102
168
  if (err) {
103
- return console.log('Fatal Error', err);
169
+ return console.error(err);
104
170
  }
105
171
 
106
172
  // Log subscribed topics
@@ -113,10 +179,10 @@ pushy.getTopics((err, topics) => {
113
179
  Retrieve a list of devices subscribed to a certain topic using the [Pub/Sub Subscribers API](https://pushy.me/docs/api/pubsub-subscribers):
114
180
 
115
181
  ```js
116
- pushy.getSubscribers('news', (err, subscribers) => {
182
+ pushy.getSubscribers('news', function (err, subscribers) {
117
183
  // Log errors to console
118
184
  if (err) {
119
- return console.log('Fatal Error', err);
185
+ return console.error(err);
120
186
  }
121
187
 
122
188
  // Log subscribed devices
@@ -126,13 +192,13 @@ pushy.getSubscribers('news', (err, subscribers) => {
126
192
 
127
193
  ## pushy.subscribe(topics, deviceToken)
128
194
 
129
- Subscribe a device to topics using the [Pub/Sub Subscribe API](https://pushy.me/docs/api/pubsub-subscribe):
195
+ Subscribe a device to one or more topics using the [Pub/Sub Subscribe API](https://pushy.me/docs/api/pubsub-subscribe):
130
196
 
131
197
  ```js
132
- pushy.subscribe(['news', 'weather'], 'TOKEN', (err) => {
198
+ pushy.subscribe(['news', 'weather'], 'DEVICE_TOKEN', function (err) {
133
199
  // Log errors to console
134
200
  if (err) {
135
- return console.log('Fatal Error', err);
201
+ return console.error(err);
136
202
  }
137
203
 
138
204
  // Log success
@@ -142,13 +208,13 @@ pushy.subscribe(['news', 'weather'], 'TOKEN', (err) => {
142
208
 
143
209
  ## pushy.unsubscribe(topics, deviceToken)
144
210
 
145
- Unsubscribe a device from topics using the [Pub/Sub Unsubscribe API](https://pushy.me/docs/api/pubsub-unsubscribe)
211
+ Unsubscribe a device from one or more topics using the [Pub/Sub Unsubscribe API](https://pushy.me/docs/api/pubsub-unsubscribe)
146
212
 
147
213
  ```js
148
- pushy.unsubscribe(['news', 'weather'], 'TOKEN', (err) => {
214
+ pushy.unsubscribe(['news', 'weather'], 'DEVICE_TOKEN', function (err) {
149
215
  // Log errors to console
150
216
  if (err) {
151
- return console.log('Fatal Error', err);
217
+ return console.error(err);
152
218
  }
153
219
 
154
220
  // Log success
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
+ }