iOS 发送 APN

linjunpop · 2012年05月09日 · 最后由 freemem 回复于 2013年04月08日 · 5213 次阅读

需要实现发送 apple push notification

https://github.com/kdonovan/apn_sender https://github.com/PRX/apn_on_rails

貌似有这两个 gem?还是有更好的? 有人用过么?可以比较一下?

我用的 apn_sender,很好哦用

没用过 apn_sender,用过 apn_on_rails,还可以。

#1 楼 @allenwei apn_sender 不支持 enhence_format,属于单向的 socket 写入,没有获取 socket 上行数据的功能。

我后来在 em_apn 上做了些改造,基于 event_machine, 性能和稳定性提高了不少,很少出现 APN 无故失效的情况了。另外用 enhence_format 也可以知道消息发出去 apple 的应答,便于发现问题及时调整。

另外一个很容易出问题的地方:订阅 Feedback 删除失效 token 时,一定要判断失效日期,否则很有可能把一个正常的 token 删除掉。

@dazuiba em-apn 在 Unicorn 上跑

RuntimeError (eventmachine not initialized: evma_connect_to_server):

Thin 上试过,没有问题

#5 楼 @linjunpop

我没有尝试将 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

@dazuiba 谢谢 详细的代码 @Richie 之前用 em_apn 有问题,就换了 grocer, 感觉目前最好用的就是这个。

#8 楼 @linjunpop 感谢推荐,已经在用 grocer。就是 github 上不去了,真蛋疼。

grocer 感觉不错!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号