-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple.js
67 lines (57 loc) · 1.69 KB
/
apple.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
/**
* @Author: Geoffrey Bauduin <[email protected]>
*/
(function () {
var apnagent = require("apnagent");
var config = require("./config.json");
var path = require("path");
var Logger = require("./logger");
function ApplePush() {
this.apns = {}
for (var index in config.ios) {
this.apns[index] = setupApn(index, config.ios[index]);
}
}
function setupApn(index, data) {
var agent = new apnagent.Agent();
var file = path.join(__dirname, data.certificate);
Logger.log("[" + index + "]", "Trying to load", file);
agent.set("pfx file", file);
agent.enable(data.env);
agent.connect(function (err) {
if (err) {
Logger.error("[" + index + "]", "Cannot connect:", err.message);
throw err;
}
else {
Logger.log("[" + index + "]", "APNAgent is connected", "(" + data.env + ")");
agent.on("message:error", function (err) {
Logger.error("[" + index + "]", "APNAgent encountered an error", err.name, ":", err.message);
});
}
});
return agent;
};
ApplePush.prototype.isValidApp = function (app) {
var a = this.apns[app];
return typeof a != "undefined" && a != null
}
ApplePush.prototype.send = function (app, data, next) {
var message = this.apns[app].createMessage().device(data.token).alert({
title: data.title,
body: data.body,
"launch-image": data.launch_image,
"action-loc-key": data.action_loc_key
});
message.expires(data.expiration);
message.badge(data.badge);
message.sound(data.sound);
message.send(function (err) {
if (err) {
Logger.error("[" + app + "]: Cannot send push notification to device [" + data.token + "]", err);
}
next(err);
})
};
module.exports = new ApplePush;
}).call(this);