Class: Rapnd::Notification
- Inherits:
-
Object
- Object
- Rapnd::Notification
- Defined in:
- lib/rapnd/notification.rb
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#content_available ⇒ Object
Returns the value of attribute content_available.
-
#custom_properties ⇒ Object
Returns the value of attribute custom_properties.
-
#device_token ⇒ Object
Returns the value of attribute device_token.
-
#sound ⇒ Object
Returns the value of attribute sound.
Instance Method Summary collapse
-
#initialize(hash) ⇒ Notification
constructor
A new instance of Notification.
- #json_payload ⇒ Object
- #payload ⇒ Object
- #to_bytes ⇒ Object
Constructor Details
#initialize(hash) ⇒ Notification
Returns a new instance of Notification.
5 6 7 8 9 10 11 |
# File 'lib/rapnd/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
#alert ⇒ Object
Returns the value of attribute alert.
3 4 5 |
# File 'lib/rapnd/notification.rb', line 3 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
3 4 5 |
# File 'lib/rapnd/notification.rb', line 3 def badge @badge end |
#content_available ⇒ Object
Returns the value of attribute content_available.
3 4 5 |
# File 'lib/rapnd/notification.rb', line 3 def content_available @content_available end |
#custom_properties ⇒ Object
Returns the value of attribute custom_properties.
3 4 5 |
# File 'lib/rapnd/notification.rb', line 3 def custom_properties @custom_properties end |
#device_token ⇒ Object
Returns the value of attribute device_token.
3 4 5 |
# File 'lib/rapnd/notification.rb', line 3 def device_token @device_token end |
#sound ⇒ Object
Returns the value of attribute sound.
3 4 5 |
# File 'lib/rapnd/notification.rb', line 3 def sound @sound end |
Instance Method Details
#json_payload ⇒ Object
23 24 25 26 27 |
# File 'lib/rapnd/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 ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/rapnd/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 ⇒ Object
29 30 31 32 |
# File 'lib/rapnd/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 |