Class: APN::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/apn/notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Notification

Returns a new instance of Notification.



5
6
7
8
9
10
11
# 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

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



3
4
5
# File 'lib/apn/notification.rb', line 3

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



3
4
5
# File 'lib/apn/notification.rb', line 3

def badge
  @badge
end

#content_availableObject

Returns the value of attribute content_available.



3
4
5
# File 'lib/apn/notification.rb', line 3

def content_available
  @content_available
end

#custom_propertiesObject

Returns the value of attribute custom_properties.



3
4
5
# File 'lib/apn/notification.rb', line 3

def custom_properties
  @custom_properties
end

#device_tokenObject

Returns the value of attribute device_token.



3
4
5
# File 'lib/apn/notification.rb', line 3

def device_token
  @device_token
end

#soundObject

Returns the value of attribute sound.



3
4
5
# File 'lib/apn/notification.rb', line 3

def sound
  @sound
end

Instance Method Details

#json_payloadObject



23
24
25
26
27
# 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

#payloadObject



13
14
15
16
17
18
19
20
21
# 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_bytesObject



29
30
31
32
# 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