Permalink
Browse files

Merge pull request #13 from MichaelBaker/master

Fix for feedback method
  • Loading branch information...
2 parents aca49ef + 962c76b commit 8c2ada46d389f07e704683d26b36a5ff54b04f49 @jpoz committed Dec 11, 2012
Showing with 18 additions and 20 deletions.
  1. +18 −20 lib/apns/core.rb
View
38 lib/apns/core.rb
@@ -8,50 +8,49 @@ module APNS
# openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
@pem = nil # this should be the path of the pem file not the contentes
@pass = nil
-
+
class << self
attr_accessor :host, :pem, :port, :pass
end
-
+
def self.send_notification(device_token, message)
n = APNS::Notification.new(device_token, message)
self.send_notifications([n])
end
-
+
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
-
+
def self.feedback
sock, ssl = self.feedback_connection
-
+
apns_feedback = []
-
- while line = sock.gets # Read lines from the socket
- line.strip!
- f = line.unpack('N1n1H140')
- apns_feedback << [Time.at(f[0]), f[2]]
+
+ 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
-
+
protected
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)
@@ -62,23 +61,22 @@ def self.open_connection
return sock, ssl
end
-
+
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
-
end

0 comments on commit 8c2ada4

Please sign in to comment.