Created
December 30, 2010 07:34
-
-
Save samvermette/759562 to your computer and use it in GitHub Desktop.
Quickly send an Apple Push Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cert = File.read(File.join(RAILS_ROOT, 'config', 'apns-dev.pem')) | |
ctx = OpenSSL::SSL::SSLContext.new | |
ctx.key = OpenSSL::PKey::RSA.new(cert, '') #set passphrase here, if any | |
ctx.cert = OpenSSL::X509::Certificate.new(cert) | |
sock = TCPSocket.new('gateway.sandbox.push.apple.com', 2195) #development gateway | |
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx) | |
ssl.connect | |
payload = {"aps" => {"alert" => "Oh hai!", "badge" => 1, "sound" => 'default'}} | |
json = payload.to_json() | |
token = [token.delete(' ')].pack('H*') #something like 2c0cad 01d1465 346786a9 3a07613f2 b03f0b94b6 8dde3993 d9017224 ad068d36 | |
apnsMessage = "\0\0 #{token}\0#{json.length.chr}#{json}" | |
ssl.write(apnsMessage) | |
ssl.close | |
sock.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment