Class: Unipush::Apns_push
- Inherits:
-
Object
- Object
- Unipush::Apns_push
- Defined in:
- lib/unipush.rb
Overview
APNS
Instance Attribute Summary collapse
-
#ios_cert_path ⇒ Object
Returns the value of attribute ios_cert_path.
Instance Method Summary collapse
-
#get_last_error ⇒ Object
return array of errors.
-
#get_unregistered_tokens ⇒ Object
Get unregistered tokens for app Return array(Time:timestamp, String:token).
-
#get_unsent_messages ⇒ Object
array with unsent messages ids.
-
#initialize(mode = 'production') ⇒ Apns_push
constructor
mode = production / development production - use gateway.push.apple.com and feedback.push.apple.com development - use sandbox.gateway.push.apple.com and sandbox.feedback.push.apple.com.
-
#send_messages(messages) ⇒ Object
messages = [message1, message2, …] message=[token, message=:badge=>0, :newsstand=>true, :track=>true, :add=>{:param1=>1, :param2=>2}].
Constructor Details
#initialize(mode = 'production') ⇒ Apns_push
mode = production / development production - use gateway.push.apple.com and feedback.push.apple.com development - use sandbox.gateway.push.apple.com and sandbox.feedback.push.apple.com
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/unipush.rb', line 18 def initialize(mode='production') if mode == 'production' @ios_push_url = 'gateway.push.apple.com' @ios_feedback_url = 'feedback.push.apple.com' else @ios_push_url = 'sandbox.gateway.push.apple.com' @ios_feedback_url = 'sandbox.feedback.push.apple.com' end @ios_push_port = '2195' @ios_feedback_port = '2196' @ios_cert_path = '' @last_error = [] @unsent_messages = [] @sock = nil @ssl = nil @apns_feedback_conn = nil @logger = Logger.new(Rails.root.join('log', 'background_custom.log'), shift_age=7, shift_size=1048576) @logger.level = Logger::INFO end |
Instance Attribute Details
#ios_cert_path ⇒ Object
Returns the value of attribute ios_cert_path.
11 12 13 |
# File 'lib/unipush.rb', line 11 def ios_cert_path @ios_cert_path end |
Instance Method Details
#get_last_error ⇒ Object
return array of errors
45 46 47 |
# File 'lib/unipush.rb', line 45 def get_last_error @last_error.empty? ? false : @last_error end |
#get_unregistered_tokens ⇒ Object
Get unregistered tokens for app
Return array(Time:, String:token)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/unipush.rb', line 100 def get_unregistered_tokens cert_path = @ios_cert_path if FileTest.exist?(cert_path) begin @logger.info(Time.now.to_s + " -- start getting error tokens") certificate = File.read(cert_path) context = OpenSSL::SSL::SSLContext.new context.key = OpenSSL::PKey::RSA.new(certificate) context.cert = OpenSSL::X509::Certificate.new(certificate) # получим удаленные токены sock = TCPSocket.new("feedback.push.apple.com", 2196) ssl = OpenSSL::SSL::SSLSocket.new(sock,context) ssl.connect apns_feedback = [] while line = ssl.read(38) line.strip! f = line.unpack("NnH*") apns_feedback << [Time.at(f[0]), f[2]] end ssl.close sock.close # и вернем их для удаления ret = [] unless apns_feedback.empty? apns_feedback.each do |ff| ret.push(ff[1]) end end @logger.info(Time.now.to_s + " -- got error tokens #{ret.size}") ret rescue @last_error.push("Could not get tokens. Exception: #{$!.inspect}") false end else @last_error.push("Certificate file does not exist") false end end |
#get_unsent_messages ⇒ Object
array with unsent messages ids
52 53 54 |
# File 'lib/unipush.rb', line 52 def @unsent_messages.nil? ? false : @unsent_messages end |
#send_messages(messages) ⇒ Object
messages = [message1, message2, …] message=[token, message=:badge=>0, :newsstand=>true, :track=>true, :add=>{:param1=>1, :param2=>2}]
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/unipush.rb', line 60 def () @logger.info(Time.now.to_s + " -- start sending ios") begin .each_with_index do |m, k| mes = (m[0], m[1], k) if mes if apns_check_conn begin tmp = @ssl.read_nonblock(6) reply = tmp.unpack("CCN") @unsent_messages.push(reply[2]) @logger.error(Time.now.to_s + " -- error in message") @last_error.push("Could not send message #{reply[2]} width error: "+reply.join(", ")) self.apns_close rescue IO::WaitReadable # no op rescue EOFError self.apns_close end @ssl.write(mes) @logger.info(Time.now.to_s + " -- sent one message") end else @unsent_messages = [] if @unsent_messages.nil? @unsent_messages.push(k) end end rescue @last_error.push("Could not send messages. Exception: #{$!.inspect}") false else true end end |