class ApnMachine::Notification

Constants

PAYLOAD_MAX_BYTES

Attributes

alert[RW]
badge[RW]
custom[RW]
device_token[RW]
sound[RW]

Public Class Methods

to_bytes(encoded_payload) click to toggle source
# File lib/apnmachine/notification.rb, line 31
def self.to_bytes(encoded_payload)
  notif_hash = ActiveSupport::JSON.decode(encoded_payload)
  
  device_token = notif_hash.delete('device_token')
  bin_token = [device_token].pack('H*')
  raise NoDeviceToken.new("No device token") unless device_token
  
  j = ActiveSupport::JSON.encode(notif_hash)
  raise PayloadTooLarge.new("The payload is larger than allowed: #{j.length}") if j.size > PAYLOAD_MAX_BYTES
  
  Config.logger.debug "TOKEN:#{device_token} | ALERT:#{notif_hash.inspect}"
  
  [0, 0, bin_token.size, bin_token, 0, j.size, j].pack("ccca*cca*")
end

Public Instance Methods

encode_payload() click to toggle source
# File lib/apnmachine/notification.rb, line 10
def encode_payload
  p = {:aps => Hash.new}
  [:badge, :alert, :sound].each do |k|
    p[:aps][k] = send(k) if send(k)
  end
  p.merge!(custom) if send(:custom)
  
  j = ActiveSupport::JSON.encode(p)
  raise PayloadTooLarge.new("The payload is larger than allowed: #{j.length}") if j.size > PAYLOAD_MAX_BYTES
  
  p[:device_token] = device_token
  raise NoDeviceToken.new("No device token") unless device_token
  
  ActiveSupport::JSON.encode(p)
end
push() click to toggle source
# File lib/apnmachine/notification.rb, line 26
def push
  raise 'No Redis client' if Config.redis.nil?
  socket = Config.redis.rpush "apnmachine.queue", encode_payload
end