Class: Unipush::Apns_push

Inherits:
Object
  • Object
show all
Defined in:
lib/unipush.rb

Overview

APNS

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject

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_errorObject

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_tokensObject

Get unregistered tokens for app

Return array(Time:timestamp, 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_messagesObject

array with unsent messages ids



52
53
54
# File 'lib/unipush.rb', line 52

def get_unsent_messages
  @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 send_messages(messages)
  @logger.info(Time.now.to_s + " -- start sending ios")
  begin
    messages.each_with_index do |m, k|
      mes = prepare_ios_message(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