node-apn

 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
var apn = require('apn');
var path = require('path');
var options = {
key : path.join(process.cwd(), sails.config.notificator.key),
cert : path.join(process.cwd(), sails.config.notificator.cert),
production : sails.config.notificator.notSandbox,//true if not "sandbox"
passphrase : sails.config.notificator.passphrase,
rejectUnauthorized: false
};
var apnConnection = new apn.Connection(options);
var types = new Object({
'validateRequest' : 1,
'validateApprove': 2,
'validateDecline': 3,
'shareRequest': 4,
'shareApprove': 5,
'shareDecline': 6,
'test': 7
});
module.exports = {
send : function (to, type, text) {
var noteType = types[type];
if (to.devices && to.devices.length) {
for (var i = 0; i < to.devices.length; i++) {
if (to.devices[i].token) {
try {
var myDevice = new apn.Device(to.devices[i].token);
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 200;
note.sound = "ping.aiff";
note.payload = {
'aps' : {
'alert' : text,
'badge': 1
},
'type' : noteType
};
apnConnection.pushNotification(note, myDevice);
} catch (e) {
console.log("WAS ERROR WHEN CALLED NOTIFICATION");
return;
}
}
}
}
}
};