Class: APNs4r::Sender

Inherits:
ApnsConnection show all
Defined in:
lib/sender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Sender

Creates new APNs4r::Sender object with given host and port

Accepts params in 2 ways, either as 2 strings : or as a Hash of optional arguments: :host => [String] host default to APNs sandbox :port => [Fixnum] port don't think it can change, just in case :apns4r_cert_file => [String] path to cert file (used to support multiple iphone applications from one server) :apns4r_cert_key => [String] path to cert key (as above)

Parameters:

  • host (String)

    default to APNs sandbox

  • port (Fixnum)

    don't think it can change, just in case


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sender.rb', line 18

def initialize *args
  
  if args[0].is_a? Hash
    options = args[0]
    @host = options.delete(:host) || OPTIONS[:apns4r_push_host]
    @port = options.delete(:port) || OPTIONS[:apns4r_push_port]
    
    @ssl ||= connect(@host, @port, options)
  else
    @host = args[0] || OPTIONS[:apns4r_push_host]
    @port = args[1] || OPTIONS[:apns4r_push_port]

    @ssl ||= connect(@host, @port)
  end
  self
end

Instance Attribute Details

#hostObject

Returns the value of attribute host


5
6
7
# File 'lib/sender.rb', line 5

def host
  @host
end

#portObject

Returns the value of attribute port


5
6
7
# File 'lib/sender.rb', line 5

def port
  @port
end

Instance Method Details

#close_connectionObject


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

def close_connection
  @ssl.close
  @ssl = nil
end

#push(notification) ⇒ Object

sends Notification object to Apple's server n = APNs4r::Notification.create 'e754dXXXX…', { :aps => => “Hey, dude!”, :badge => 1, :custom_data => “asd” } sender = APNs4r::Sender.new.push n

Parameters:


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sender.rb', line 40

def push notification
  delay = 2
  begin
    @ssl.write notification.to_s
  rescue OpenSSL::SSL::SSLError, Errno::EPIPE
    sleep delay
    @ssl = connect(@host, @port)
    delay*=2 and retry if delay < 60
    raise Timeout::Error
  end
end