forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aps-helper.js
50 lines (41 loc) · 1.64 KB
/
aps-helper.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
var apn = require('apn'),
app = require('./app'),
logger = require('./logger');
var apnConnection = new apn.Connection(app.config.apn);
apnConnection.on('connected', function() {
logger.info("openHAB-cloud: APN connected");
});
apnConnection.on('transmitted', function(notification, device) {
logger.info("APN notification transmitted to:" + device.token.toString('hex'));
});
apnConnection.on('transmissionError', function(errCode, notification, device) {
logger.error("openHAB-cloud: APN notification caused error: " + errCode + " for device ", device, notification);
});
apnConnection.on('timeout', function () {
logger.error("openHAB-cloud: APN connection Timeout");
});
apnConnection.on('disconnected', function() {
logger.error("openHAB-cloud: APN disconnected");
});
apnConnection.on('socketError', logger.error);
module.exports.test = function(deviceToken) {
var myDevice = new apn.Device(deviceToken);
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 0;
note.sound = "ping.aiff";
note.alert = {};
note.alert.body = "openHAB is offline";
note.payload = {'messageFrom': 'Caroline'};
apnConnection.pushNotification(note, myDevice);
}
module.exports.sendAppleNotification = function(deviceToken, message) {
var myDevice = new apn.Device(deviceToken);
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 0;
note.sound = "ping.aiff";
note.alert = {};
note.alert.body = message;
apnConnection.pushNotification(note, myDevice);
}