需要实现发送 apple push notification
https://github.com/kdonovan/apn_sender https://github.com/PRX/apn_on_rails
貌似有这两个 gem?还是有更好的? 有人用过么?可以比较一下?
@dazuiba em-apn 在 Unicorn 上跑
RuntimeError (eventmachine not initialized: evma_connect_to_server):
Thin 上试过,没有问题
我没有尝试将 em-apn 放到 thin/mongrel/unicorn 上。我认为这么做貌似不太合适。
我的做法是将 em-apn 做成一个独立的进程,并使用 EM.add_periodic_time 每秒从 JobQueue 中取一个 APNS 任务。
代码如下:
EM.run do
client = EM::APN::Client.connect
# client.deliver(EM::APN::Notification.new(token, "alert" => alert))
client.on_error do |response|
EM::APN.logger.info "response #{response.inspect}"
end
max_reconnect = MAX_RECONNECT
client.on_close do
if $running
if max_reconnect>0
EM::APN.logger.info "attempt reconnect, reconnect avaliable: #{max_reconnect}"
conn = client.connection
conn.close_connection
client.connect
EM::APN.logger.info "success reconnected!"
max_reconnect-=1
else
EM::APN.logger.info "max reconnect reached, exsit"
EM.stop_event_loop
end
end
end
# Hopefully give ourselves enough time to receive a response on failure.
# Wish there was a better way to do this. Or at least a more timely way.
EM.add_periodic_timer($interval) {
while job = $em_apns_worker.fetch_job
client.deliver(job.to_notification)
end
}
end