class Apns::Persistent::PushClient

Public Class Methods

gateway_uri(sandbox) click to toggle source
# File lib/apns/persistent/push_client.rb, line 80
def gateway_uri(sandbox)
  sandbox ? "apn://gateway.sandbox.push.apple.com:2195" : "apn://gateway.push.apple.com:2195"
end
message(token, alert, badge, sound, category, content_available, mutable_content, custom_payload, id, expiry, priority) click to toggle source
# File lib/apns/persistent/push_client.rb, line 84
def message(token, alert, badge, sound, category, content_available, mutable_content, custom_payload, id, expiry, priority)
  data = [token_data(token),
          payload_data(custom_payload, alert, badge, sound, category, content_available, mutable_content),
          id_data(id),
          expiration_data(expiry),
          priority_data(priority)].compact.join
  [2, data.bytes.count, data].pack('cNa*')
end
push_once(certificate: , passphrase: nil, sandbox: true, token: , alert: nil, badge: nil, sound: nil, category: nil, content_available: true, mutable_content: false, custom_payload: nil, id: nil, expiry: nil, priority: nil) { |command, status, return_id| ... } click to toggle source
# File lib/apns/persistent/push_client.rb, line 6
def self.push_once(certificate: ,
                   passphrase: nil,
                   sandbox: true,
                   token: ,
                   alert: nil,
                   badge: nil,
                   sound: nil,
                   category: nil,
                   content_available: true,
                   mutable_content: false,
                   custom_payload: nil,
                   id: nil,
                   expiry: nil,
                   priority: nil)

  client = PushClient.new(certificate: certificate, passphrase: passphrase, sandbox: sandbox)
  client.open

  client.push(token: token,
              alert: alert,
              badge: badge,
              sound: sound,
              category: category,
              content_available: content_available,
              mutable_content: mutable_content,
              custom_payload: custom_payload,
              id: id,
              expiry: expiry,
              priority: priority) do |command, status, return_id|
    if block_given?
      yield(command, status, return_id)
    end
  end

  client.close
end

Private Class Methods

expiration_data(expiry) click to toggle source
# File lib/apns/persistent/push_client.rb, line 118
def expiration_data(expiry)
  [4, 4, expiry.to_i].pack('cnN') unless expiry.nil?
end
id_data(id) click to toggle source
# File lib/apns/persistent/push_client.rb, line 114
def id_data(id)
  [3, 4, id].pack('cnN') unless id.nil?
end
payload_data(custom_payload, alert, badge, sound, category, content_available, mutable_content) click to toggle source
# File lib/apns/persistent/push_client.rb, line 99
def payload_data(custom_payload, alert, badge, sound, category, content_available, mutable_content)
  payload = {}.merge(custom_payload || {}).inject({}){|h,(k,v)| h[k.to_s] = v; h}

  payload['aps'] ||= {}
  payload['aps']['alert'] = alert if alert
  payload['aps']['badge'] = badge.to_i rescue 0 if badge
  payload['aps']['sound'] = sound if sound
  payload['aps']['category'] = category if category
  payload['aps']['content-available'] = 1 if content_available
  payload['aps']['mutable-content'] = 1 if mutable_content
  
  json = payload.to_json
  [2, json.bytes.count, json].pack('cna*')
end
priority_data(priority) click to toggle source
# File lib/apns/persistent/push_client.rb, line 122
def priority_data(priority)
  [5, 1, priority].pack('cnc') unless priority.nil?
end
token_data(token) click to toggle source
# File lib/apns/persistent/push_client.rb, line 95
def token_data(token)
  [1, 32, token.gsub(/[<\s>]/, '')].pack('cnH64')
end

Public Instance Methods

push(token: , alert: nil, badge: nil, sound: nil, category: nil, content_available: true, mutable_content: false, custom_payload: nil, id: nil, expiry: nil, priority: nil) { |command, status, return_id| ... } click to toggle source
# File lib/apns/persistent/push_client.rb, line 43
def push(token: ,
         alert: nil,
         badge: nil,
         sound: nil,
         category: nil,
         content_available: true,
         mutable_content: false,
         custom_payload: nil,
         id: nil,
         expiry: nil,
         priority: nil)
  
  raise 'please open' if closed?

  m = PushClient.message(token, alert, badge, sound, category, content_available, mutable_content, custom_payload, id, expiry, priority)
  @connection.write(m)

  if block_given? && @connection.readable?
    if error = @connection.read(6)
      command, status, return_id = error.unpack('ccN')
      yield(command, status, return_id)
      @connection.reopen
    end
  end
end
register_error_handle() { |command, status, id| ... } click to toggle source
# File lib/apns/persistent/push_client.rb, line 69
def register_error_handle
  Thread.new do
    while error = @connection.read(6)
      command, status, id = error.unpack('ccN')
      yield(command, status, id)
      @connection.reopen
    end
  end
end