Using node-apn
Multi push to devices
node-apn link: https://github.com/argon/node-apn
GitHub example on node-apn :
https://github.com/argon/node-apn/blob/master/examples/sending-to-multiple-devices.js
GitHub example on node-apn :
https://github.com/argon/node-apn/blob/master/examples/sending-to-multiple-devices.js
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; | |
} | |
}; |
댓글 없음:
댓글 쓰기