-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
51 lines (40 loc) · 1.26 KB
/
index.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
'use strict';
var apn = require('apn');
var _ = require('lodash');
function createDeviceTokens(token) {
if(!token || _.isEmpty(token)){
throw new Error('Device token is required');
}
// Array of tokens
if(_.isArray(token)){
return token.map(function(token) {
return new apn.Device(token);
});
}
// Simple length validation for the token length
if(token.length !== 64){
throw new Error('Device token should be 64 characters');
}
return new apn.Device(token);
}
module.exports = function(message, options, callback){
var defaultOptions = {
cert: 'cert.pem',
key: 'key.pem',
fastMode: true,
production: true,
connectionTimeout: 1000
};
options = _.defaults(options, defaultOptions);
callback = callback || _.noop;
var device = createDeviceTokens(options.token);
var connection = new apn.Connection(options);
var notification = new apn.Notification();
notification.alert = message || 'Hello world!';
notification.badge = options.badge || 0;
notification.sound = options.sound || 'ping.aiff';
notification.payload = options.payload || {};
notification.expiry = options.expiry || Math.floor(Date.now() / 1000) + 3600;
connection.pushNotification(notification, device);
return callback(connection);
};