Created
December 9, 2013 08:33
-
-
Save Blankwonder/7869121 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 redis = require('redis'); | |
redis.debug_mode = true; | |
var db = redis.createClient(process.env.NODE_ENV === 'development' ? '6379' : '6392'); | |
var apns = require('apn'); | |
var util = require('util'); | |
var errorCallback = function(errcode, err) { | |
console.log(errcode, err); | |
}; | |
var iosOption = { | |
cert: __dirname + '/keys/ios_cert.pem', | |
key: __dirname + '/keys/ios_key.pem', | |
errorCallback: errorCallback | |
}; | |
var iosSandboxOption = { | |
cert: __dirname + '/keys/ios_sandbox_cert.pem', | |
key: __dirname + '/keys/ios_sandbox_key.pem', | |
gateway: 'gateway.sandbox.push.apple.com', | |
errorCallback: errorCallback | |
}; | |
var apnConnections = { | |
ios: new apns.Connection(iosOption), | |
iosSandbox: new apns.Connection(iosSandboxOption) | |
}; | |
doloop = function() { | |
db.brpop('queue:push.IOS_SANDBOX', 'queue:push.IOS', 0, function(err, info) { | |
if (!err) { | |
var connection; | |
var key = info[0]; | |
if (key === 'queue:push.IOS_SANDBOX') { | |
connection = apnConnections.iosSandbox; | |
} else if (key === 'queue:push.IOS') { | |
connection = apnConnections.ios; | |
} | |
info = JSON.parse(info[1]); | |
if (connection) { | |
var note = new apns.Notification(); | |
note.payload = info.payload; | |
note.device = new apns.Device(info.token); | |
connection.sendNotification(note); | |
} | |
} | |
doloop(); | |
}); | |
}; | |
db.select(1, doloop); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment