apns test
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
require "base64" | |
require "openssl" | |
require "socket" | |
require "yajl" #gem install yajl-ruby | |
IDENTIFIER = 0 | |
EXPIRY = 0 | |
ENHANCED_COMMAND = 1 | |
DEVICE_TOKEN_SIZE = 32 | |
# DEVICE_TOKEN = Base64.decode64("4DJXVKUhziWwNihDfraFXXZ4RIJK+dCHoiS3awnh0w0=") | |
DEVICE_TOKEN = "4DJXVKUhziWwNihDfraFXXZ4RIJK+dCHoiS3awnh0w0=" | |
def payload | |
payload = Hash.new | |
payload['apns'] = Hash.new | |
payload['apns']['alert'] = "Hello world" | |
payload['apns']['badge'] = 10 | |
payload['apns']['sound'] = "ding.aiff" | |
Yajl::Encoder.encode(payload) | |
end | |
def to_binary(payload) | |
payload_size = payload.bytesize | |
[ENHANCED_COMMAND, IDENTIFIER, EXPIRY, 0, DEVICE_TOKEN_SIZE, DEVICE_TOKEN, 0, payload_size, payload].pack("cnnccH*cca*") #"cNNccH*cca*" - doesn't seem to work | |
end | |
def connect | |
cert = File.read('config/certs/cert_ios_production.pem') | |
ctx = OpenSSL::SSL::SSLContext.new | |
ctx.key = OpenSSL::PKey::RSA.new(cert, nil) | |
ctx.cert = OpenSSL::X509::Certificate.new(cert) | |
sock = TCPSocket.new('gateway.push.apple.com', 2195) | |
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx) | |
ssl.connect | |
return ssl, sock | |
end | |
def write(ssl, sock) | |
ssl.write(to_binary(payload)) | |
ssl.flush | |
if IO.select([ssl], nil, nil, 1) | |
error = ssl.read(6) | |
if error | |
error = error.unpack("ccN") | |
puts "ERROR: #{error}" | |
else | |
puts "NO ERROR" | |
end | |
end | |
end | |
def close(ssl, sock) | |
ssl.close | |
sock.close | |
end | |
#DO IT!! | |
ssl, sock = connect | |
write(ssl, sock) | |
close(ssl, sock) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment