class Pushr::MessageApns

Constants

POSTFIX

Attributes

attributes_for_device[RW]
badge[RW]
category[RW]
content_available[RW]
device[RW]
expiry[RW]
priority[RW]
sound[RW]

Public Instance Methods

alert() click to toggle source
# File lib/pushr/message_apns.rb, line 22
def alert
  string_or_json = @alert
  return MultiJson.load(string_or_json)
rescue
  return string_or_json
end
alert=(alert) click to toggle source
# File lib/pushr/message_apns.rb, line 14
def alert=(alert)
  if alert.is_a?(Hash)
    @alert = MultiJson.dump(alert)
  else
    @alert = alert
  end
end
id() click to toggle source
# File lib/pushr/message_apns.rb, line 29
def id
  @id ||= OpenSSL::Random.random_bytes(4)
end
payload() click to toggle source
# File lib/pushr/message_apns.rb, line 43
def payload
  MultiJson.dump(as_json)
end
payload_size() click to toggle source
# File lib/pushr/message_apns.rb, line 47
def payload_size
  payload.bytesize
end
to_hash() click to toggle source
# File lib/pushr/message_apns.rb, line 51
def to_hash
  hsh = { type: self.class.to_s, app: app, device: device, alert: alert, badge: badge, sound: sound, expiry: expiry,
          category: category, attributes_for_device: attributes_for_device, priority: priority,
          content_available: content_available }
  hsh[Pushr::Core.external_id_tag] = external_id if external_id
  hsh
end
to_message() click to toggle source
# File lib/pushr/message_apns.rb, line 33
def to_message
  data = ''
  data << [1, [device].pack('H*').bytesize, [device].pack('H*')].pack('CnA*')
  data << [2, payload.bytesize, payload].pack('CnA*')
  data << [3, id.bytesize, id].pack('CnA*')
  data << [4, 4, expiry].pack('CnN')
  data << [5, 1, priority].pack('CnC')
  ([2, data.bytesize].pack('CN') + data)
end

Private Instance Methods

as_json() click to toggle source
# File lib/pushr/message_apns.rb, line 61
def as_json
  json = ActiveSupport::OrderedHash.new
  json['aps'] = ActiveSupport::OrderedHash.new
  json['aps']['alert'] = alert if alert
  json['aps']['badge'] = badge if badge
  json['aps']['sound'] = sound if sound
  json['aps']['category'] = category if category
  json['aps']['content-available'] = content_available if content_available
  attributes_for_device.each { |k, v| json[k.to_s] = v.to_s } if attributes_for_device
  json
end
max_payload_size() click to toggle source
# File lib/pushr/message_apns.rb, line 73
def max_payload_size
  if payload_size > 2048
    errors.add(:payload, 'APN notification cannot be larger than 2048 bytes. Try condensing your alert and attributes.')
  end
end
priority_with_content_available() click to toggle source
# File lib/pushr/message_apns.rb, line 79
def priority_with_content_available
  if content_available == 1 && priority != 5 && !(alert || badge || sound)
    errors.add(:priority, 'Priority should be 5 if content_available = 1 and no alert/badge/sound')
  end
end