Module: APNS

Defined in:
lib/apns/core.rb,
lib/apns/notification.rb

Defined Under Namespace

Classes: Notification

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.hostObject

Returns the value of attribute host.



13
14
15
# File 'lib/apns/core.rb', line 13

def host
  @host
end

.passObject

Returns the value of attribute pass.



13
14
15
# File 'lib/apns/core.rb', line 13

def pass
  @pass
end

.pemObject

Returns the value of attribute pem.



13
14
15
# File 'lib/apns/core.rb', line 13

def pem
  @pem
end

.portObject

Returns the value of attribute port.



13
14
15
# File 'lib/apns/core.rb', line 13

def port
  @port
end

Class Method Details

.feedbackObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/apns/core.rb', line 32

def self.feedback
  sock, ssl = self.feedback_connection

  apns_feedback = []

  while message = ssl.read(38)
    timestamp, token_size, token = message.unpack('N1n1H*')
    apns_feedback << [Time.at(timestamp), token]
  end

  ssl.close
  sock.close

  return apns_feedback
end

.feedback_connectionObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/apns/core.rb', line 65

def self.feedback_connection
  raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
  raise "The path to your pem file does not exist!" unless File.exist?(self.pem)

  context      = OpenSSL::SSL::SSLContext.new
  context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
  context.key  = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)

  fhost = self.host.gsub('gateway','feedback')
  puts fhost

  sock         = TCPSocket.new(fhost, 2196)
  ssl          = OpenSSL::SSL::SSLSocket.new(sock,context)
  ssl.connect

  return sock, ssl
end

.open_connectionObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/apns/core.rb', line 50

def self.open_connection
  raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
  raise "The path to your pem file does not exist!" unless File.exist?(self.pem)

  context      = OpenSSL::SSL::SSLContext.new
  context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
  context.key  = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)

  sock         = TCPSocket.new(self.host, self.port)
  ssl          = OpenSSL::SSL::SSLSocket.new(sock,context)
  ssl.connect

  return sock, ssl
end

.send_notification(device_token, message) ⇒ Object



16
17
18
19
# File 'lib/apns/core.rb', line 16

def self.send_notification(device_token, message)
  n = APNS::Notification.new(device_token, message)
  self.send_notifications([n])
end

.send_notifications(notifications) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/apns/core.rb', line 21

def self.send_notifications(notifications)
  sock, ssl = self.open_connection

  notifications.each do |n|
    ssl.write(n.packaged_notification)
  end

  ssl.close
  sock.close
end