-
Notifications
You must be signed in to change notification settings - Fork 48
/
transport.js
196 lines (158 loc) · 5.62 KB
/
transport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var mandrill = require('node-mandrill');
var nodemailer = require('nodemailer');
var twilio = require('twilio');
var gcm = require('node-gcm');
var apn = require('apn');
var logger = require('./utils/logger');
var config = require('../config');
var setupMandrill = function () {
if (!validConfig()) {
var errorMsg = 'missing mandrill token, please update config.transport.mandrill section';
logger.error(errorMsg);
throw new Error(errorMsg);
}
return mandrill(config.transport.mandrill.token);
function validConfig() {
return config.transport.mandrill && config.transport.mandrill.token;
}
};
var setupNodeMailer = function () {
if(!validConfig()) {
var errorMsg = 'missing mailer config, please update config.transport.nodemailer section';
logger.error(errorMsg);
throw new Error(errorMsg);
}
let transporter = nodemailer.createTransport({
host: config.transport.nodemailer.host,
port: config.transport.nodemailer.port,
secure: config.transport.nodemailer.secure, // true for 465, false for other ports
auth: {
user: config.transport.nodemailer.auth.user,
pass: config.transport.nodemailer.auth.pass
}
});
return transporter;
function validConfig() {
return config.transport.nodemailer && (config.transport.nodemailer.host && config.transport.nodemailer.port && config.transport.nodemailer.auth)
}
};
var setupTwilio = function () {
if (!validConfig()) {
var errorMsg = 'missing twilio account SID or auth Token, please update config.transport.twilio section';
logger.error(errorMsg);
throw new Error(errorMsg);
}
return twilio(config.transport.twilio.accountSid, config.transport.twilio.authToken);
function validConfig() {
return config.transport.twilio && (config.transport.twilio.accountSid && config.transport.twilio.authToken);
}
};
var setupAndroidPushNotification = function () {
if(!validConfig()) {
throw new Error('missing server api key, please update config.transport.gcm.serverApiKey section');
}
return {
push: push
};
function push(options, callback) {
if(!validOptions()) {
var errorMsg = "missing 'options' or required required fields, please make sure you have provided 'options'";
logger.error(errorMsg);
throw new Error(errorMsg);
}
var service = new gcm.Sender(config.transport.gcm.serverApiKey);
var message = new gcm.Message({
collapseKey: 'notifier',
timeToLive: 3600 * 2,
delayWhileIdle: false
});
message.addDataWithObject(options.message);
service.send(message, options.regIds, options.retries, callback);
function validOptions() {
return options.message && options.regIds && options.retries;
}
}
function validConfig() {
return config.transport.gcm && config.transport.gcm.serverApiKey;
}
};
var setupIOSPushNotification = function () {
if(!validConfig()) {
var errorMsg = "missing 'cert.pem' or 'key.pem', please update 'config.transport.apn section'";
logger.error(errorMsg);
throw new Error(errorMsg);
}
return {
push: push
};
function validConfig() {
return config.transport.apn && (config.transport.apn.cert && config.transport.apn.key);
}
function push(options, callback) {
var service, note;
var productionGateway = 'gateway.push.apple.com',
developmentGateway = 'gateway.sandbox.push.apple.com';
if(!validOptions()) {
var errorMsg = "missing 'options' or required fields, please make sure you have that options are defined";
logger.error(errorMsg);
throw new Error(errorMsg);
}
initConnection();
initNotification();
service.pushNotification(note, options.tokens);
function validOptions() {
return options && options.alert && (options.tokens && options.tokens.length > 0);
}
function initConnection() {
service = new apn.connection({
production: options.production,
gateway: options.production ? productionGateway : developmentGateway,
port: options.port || 2195,
enhanced: options.enhanced || true,
cacheLength: options.cacheLength || 100,
// errorCallback: callback,
cert: config.transport.apn.cert,
key: config.transport.apn.key,
passphrase: options.passphrase
});
service.on('connected', function() {
logger.info('APN Connected.');
});
service.on('transmitted', function(notification, device) {
return callback(null, "Notification transmitted to:" + device.token.toString('hex'));
});
service.on('transmissionError', function(errCode, notification, device) {
if(errCode === 8) {
var errorMsg = 'A error code of 8 indicates that the device token is invalid. This could be for a number of reasons - are you using the correct environment? i.e. Production vs. Sandbox';
logger.error(errorMsg);
return callback(errorMsg);
}
return callback('Notification caused error: ' + errCode + ' for device ', device, notification);
});
service.on('timeout', function() {
var errorMsg = 'APNS connection timeout';
logger.warning(errorMsg);
return callback(errorMsg);
});
service.on('socketError', function() {
var errorMsg = 'APNS socket error';
logger.error(errorMsg);
});
}
function initNotification() {
note = new apn.notification();
note.sound = options.sound || 'notification-beep.wav';
note.alert = options.alert || { "body" : "Place your message here.", "action-loc-key" : "Play" , "launch-image" : "mysplash.png"};
note.payload = options.payload || {'messageFrom': 'Notifier'};
note.badge = options.badge;
}
}
};
var transport = {
mandrill: setupMandrill(),
nodemailer: setupNodeMailer(),
twilio: setupTwilio(),
android: setupAndroidPushNotification(),
ios: setupIOSPushNotification()
};
module.exports = transport;