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;
}
}
}
}
}
};