Skip to content

Commit fd9a375

Browse files
committed
Native push notifications
1 parent ae07696 commit fd9a375

File tree

5 files changed

+77
-13
lines changed

5 files changed

+77
-13
lines changed

lib/config.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@ var Token = require('./token');
33
function Config(options) {
44
options = options || {};
55

6-
if (options.host) {
7-
this.host = options.host
8-
}
9-
else if (options.cluster) {
10-
this.host = "api-"+options.cluster+".pusher.com";
11-
}
12-
else {
13-
this.host = "api.pusherapp.com";
14-
}
15-
166
this.scheme = options.scheme || (options.encrypted ? "https" : "http");
177
this.port = options.port;
188

@@ -25,7 +15,7 @@ function Config(options) {
2515
}
2616

2717
Config.prototype.prefixPath = function(subPath) {
28-
return "/apps/" + this.appId + subPath;
18+
throw("NotImplementedError: #prefixPath should be implemented by subclasses");
2919
};
3020

3121
Config.prototype.getBaseURL = function(subPath, queryString) {

lib/notification_client.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var Config = require('./config');
2+
var requests = require('./requests');
3+
var util = require('./util');
4+
var NotificationConfig = require('./notification_config');
5+
6+
function NotificationClient(options){
7+
this.config = new NotificationConfig(options);
8+
}
9+
10+
NotificationClient.prototype.notify = function(interests, notification, callback) {
11+
var body = util.mergeObjects({interests: interests}, notification);
12+
requests.send(this.config, {
13+
method: "POST",
14+
body: body,
15+
path: "/notification"
16+
}, callback);
17+
}
18+
19+
module.exports = NotificationClient;

lib/notification_config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var Config = require('./config');
2+
var util = require('./util');
3+
4+
var DEFAULT_HOST = "yolo.ngrok.io";
5+
var API_PREFIX = "customer_api";
6+
var API_VERSION = "v1";
7+
8+
function NotificationConfig(options) {
9+
Config.call(this, options);
10+
this.host = options.host || DEFAULT_HOST;
11+
}
12+
13+
util.mergeObjects(NotificationConfig.prototype, Config.prototype);
14+
15+
NotificationConfig.prototype.prefixPath = function(subPath) {
16+
return "/" + API_PREFIX + "/" + API_VERSION + "/apps/" + this.appId + subPath;
17+
};
18+
19+
module.exports = NotificationConfig;

lib/pusher.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ var events = require('./events');
66
var requests = require('./requests');
77
var util = require('./util');
88

9-
var Config = require('./config');
9+
var PusherConfig = require('./pusher_config');
1010
var Token = require('./token');
1111
var WebHook = require('./webhook');
12+
var NotificationClient = require('./notification_client');
1213

1314
var validateChannel = function(channel) {
1415
if (typeof channel !== "string" || channel === "" || channel.match(/[^A-Za-z0-9_\-=@,.;]/)) {
@@ -38,7 +39,9 @@ var validateSocketId = function(socketId) {
3839
* @constructor
3940
* @param {Object} options
4041
* @param {String} [options.host="api.pusherapp.com"] API hostname
42+
* @param {String} [options.notification_host="api.pusherapp.com"] Notification API hostname
4143
* @param {Boolean} [options.encrypted=false] whether to use SSL
44+
* @param {Boolean} [options.notification_encrypted=false] whether to use SSL for notifications
4245
* @param {Integer} [options.port] port, default depends on the scheme
4346
* @param {Integer} options.appId application ID
4447
* @param {String} options.key application key
@@ -48,7 +51,12 @@ var validateSocketId = function(socketId) {
4851
* @param {Boolean} [options.keepAlive] whether requests should use keep-alive
4952
*/
5053
function Pusher(options) {
51-
this.config = new Config(options);
54+
this.config = new PusherConfig(options);
55+
var notificationOptions = util.mergeObjects({}, options, {
56+
host: options.notification_host || "yolo.ngrok.io",
57+
encrypted: options.notification_encrypted
58+
});
59+
this.notificationClient = new NotificationClient(notificationOptions);
5260
}
5361

5462
/** Create a Pusher instance using a URL.
@@ -156,6 +164,10 @@ Pusher.prototype.triggerBatch = function(batch, callback) {
156164
events.triggerBatch(this, batch, callback);
157165
}
158166

167+
Pusher.prototype.notify = function() {
168+
this.notificationClient.notify.apply(this.notificationClient, arguments);
169+
}
170+
159171
/** Makes a POST request to Pusher, handles the authentication.
160172
*
161173
* Calls back with three arguments - error, request and response. When request

lib/pusher_config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var Config = require('./config');
2+
var util = require('./util');
3+
4+
function PusherConfig(options) {
5+
Config.call(this, options);
6+
7+
if (options.host) {
8+
this.host = options.host
9+
}
10+
else if (options.cluster) {
11+
this.host = "api-"+options.cluster+".pusher.com";
12+
}
13+
else {
14+
this.host = "api.pusherapp.com";
15+
}
16+
}
17+
18+
util.mergeObjects(PusherConfig.prototype, Config.prototype);
19+
20+
PusherConfig.prototype.prefixPath = function(subPath) {
21+
return "/apps/" + this.appId + subPath;
22+
};
23+
24+
module.exports = PusherConfig;

0 commit comments

Comments
 (0)