class APN::Notification

Attributes

alert[RW]
badge[RW]
content_available[RW]
custom_properties[RW]
device_token[RW]
sound[RW]

Public Class Methods

new(hash) click to toggle source
# File lib/apn/notification.rb, line 5
def initialize(hash)
  [:badge, :alert, :sound, :device_token, :content_available, :custom_properties].each do |k|
    self.instance_variable_set("@#{k}".to_sym, hash[k]) if hash[k]
  end
  raise "Must provide device token: #{hash}" if self.device_token.nil?
  self.device_token = self.device_token.delete(' ')
end

Public Instance Methods

json_payload() click to toggle source
# File lib/apn/notification.rb, line 23
def json_payload
  j = ActiveSupport::JSON.encode(payload)
  raise "The payload #{j} is larger than allowed: #{j.length}" if j.size > 256
  j
end
payload() click to toggle source
# File lib/apn/notification.rb, line 13
def payload
  p = Hash.new
  [:badge, :alert, :sound, :content_available].each do |k|
    p[k.to_s.gsub('_','-').to_sym] = send(k) if send(k)
  end
  aps = {:aps => p}
  aps.merge!(custom_properties) if custom_properties
  aps
end
to_bytes() click to toggle source
# File lib/apn/notification.rb, line 29
def to_bytes
  j = json_payload
  [0, 0, 32, self.device_token, 0, j.bytesize, j].pack("cccH*cca*").force_encoding('ASCII-8BIT')
end