Skip to content

Instantly share code, notes, and snippets.

@holoduke
Created January 25, 2019 14:09
Show Gist options
  • Save holoduke/18be175cb356b1a1f9a47428f5a0cd80 to your computer and use it in GitHub Desktop.
Save holoduke/18be175cb356b1a1f9a47428f5a0cd80 to your computer and use it in GitHub Desktop.
var apn = require('apn');
var log = {
info : function(msg){console.log(new Date()+ " - info - "+msg)},
error : function(msg){console.log(new Date()+" - error - "+msg)}
}
var options = { };
options.gateway = 'gateway.push.apple.com';
options.cert = "aps-prod.pem";
options.key = "aps-key.pem";
options.ca = ["entrust_2048_ca.cer"];
options.passphrase = "xxxxxx"
options.port = "2195";
options.rejectUnauthorized = false;
options.production = true;
var reconnect = function(){
service = new apn.Provider(options);
}
var service = new apn.Provider(options);
log.info("created service");
service.on('connected', function() {
log.info("Connected");
});
service.on('transmitted', function(notification, device) {
//log.info("Notification transmitted to:" + device.token.toString('hex'));
});
service.on('transmissionError', function(errCode, notification, device) {
console.error("Notification caused error: " + errCode + " for device ", device, notification);
if (errCode == 8) {
log.info("A error code of 8 indicates that the device token is invalid. This could be for a number of reasons - are you using the correct environment? i.e. Production vs. Sandbox");
}
});
service.on('timeout', function () {
log.info("Connection Timeout");
});
service.on('disconnected', function() {
log.info("Disconnected from APNS");
reconnect();
});
service.on('socketError', console.error);
send = function(message,tokens){
var note = new apn.Notification();
note.alert = {};
note.alert.body = "";
note.category = "SoccerCategory";
note.alert['loc-key'] = "%@\n%@"; //TODO: translate match started
note.alert['loc-args'] = [message.title,message.score];
note.alert['title'] = "Goal!";
if (message.type == "MT"){
note.alert['loc-key'] = "%@ - %@, match started!"; //TODO: translate match started
note.alert['loc-args'] = [message.home,message.away];
note.alert['title'] = "Match started";
}
else if (message.type == "FT"){
note.alert['loc-key'] = "%@ - %@, match ended!"; //TODO: translate match started
note.alert['loc-args'] = [message.home,message.away];
note.alert['title'] = "Match ended";
}
var scorehome = 0;
var scoreaway = 0;
try{
var parts = message.score.replace(" ","").trim().split("-");
scorehome = parts[0] || "0";
scoreaway = parts[1] || "0";
}
catch(e){
console.log("error sending push notification ",e);
}
note.sound = "default";
note.matchid = message.matchid;
note.localteam = message.home;
note.visitorteam = message.away;
note.localscore = scorehome;
note.visitorscore = scoreaway;
//note.badge = 1;
service.send(note, tokens);
log.info(tokens.length+" ios notifications send");
}
send({"emptymessage":1234},"0e71281ad06221dce74eef1c5f80bb79e108f6a661adf0fd932c5c45a125db55");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment