Created
January 25, 2016 13:40
-
-
Save pineoc/e1501704ea2de7304191 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var apnRequestFunc = function(dataArr, callback){ | |
//dataArr | |
/* | |
["key1", "key2", "key3"] | |
*/ | |
var apnConnection = new apn.Connection({ | |
gateway : "gateway.sandbox.push.apple.com", | |
cert: __dirname + '/../../keys/cert.pem', | |
key: __dirname + '/../../keys/key.pem', | |
production: false | |
}); | |
/* | |
* event listeners | |
* */ | |
apnConnection.on("connected", function() { | |
console.log("Connected"); | |
}); | |
apnConnection.on("transmitted", function(notification, device) { | |
console.log("Notification transmitted to:" + device.token.toString("hex")); | |
}); | |
apnConnection.on("transmissionError", function(errCode, notification, device) { | |
console.error("Notification caused error: " + errCode + " for device ", device, notification); | |
if (errCode === 8) { | |
console.log("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"); | |
} | |
}); | |
apnConnection.on("timeout", function () { | |
console.log("Connection Timeout"); | |
}); | |
apnConnection.on("disconnected", function() { | |
console.log("Disconnected from APNS"); | |
}); | |
apnConnection.on("socketError", console.error); | |
apnConnection.on('completed', function() { | |
//all request completed | |
callback(true); | |
}); | |
var tokens = []; | |
dataArr.forEach(function(elem, idx, array){ | |
var deviceToken = new apn.Device(elem); | |
tokens.push(deviceToken); | |
}); | |
var note = new apn.Notification(); | |
note.badge = 3; | |
note.alert = 'pineoc push test'; | |
note.payload = {'message': '안녕하세요'}; | |
try{ | |
apnConnection.pushNotification(note, tokens); | |
} catch (e){ | |
console.log('apn exceptions, ', e); | |
callback(false); | |
return; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment