class ApnsProviderApi::Notification

Constants

MAXIMUM_PAYLOAD_SIZE

MAXIMUM_PAYLOAD_SIZE = 4096 already accepted by provider API but not for iOS apps

Attributes

alert[RW]
badge[RW]
category[RW]
content_available[RW]
custom_data[RW]
device[RW]
device=[RW]
error_message[RW]
expiry[RW]
id[RW]
priority[RW]
sent_at[R]
sound[RW]
token[RW]
uuid[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/apns_provider_api/notification.rb, line 39
def initialize(options = {})
  @token = options.delete(:token) || options.delete(:device)
  raise 'invalid token' if @token && @token.empty?
  @badge = options.delete(:badge)
  @sound = options.delete(:sound)
  @category = options.delete(:category)
  @expiry = options.delete(:expiry)
  @id = options.delete(:id)
  @priority = options.delete(:priority)
  @content_available = options.delete(:content_available)
  dottize = options.delete(:dottize)
  @custom_data = options
  alert = options.delete(:alert)
  available_bytes = MAXIMUM_PAYLOAD_SIZE - payload.to_s.bytesize
  @alert = trim_alert(alert, available_bytes, dottize)
  generate_uuid(options.delete(:uuid))
end

Public Instance Methods

mark_as_sent!() click to toggle source
# File lib/apns_provider_api/notification.rb, line 70
def mark_as_sent!
  @sent_at = Time.now
end
mark_as_unsent!() click to toggle source
# File lib/apns_provider_api/notification.rb, line 74
def mark_as_unsent!
  @sent_at = nil
end
payload() click to toggle source
# File lib/apns_provider_api/notification.rb, line 57
def payload
  json = {}.merge(@custom_data || {}).inject({}){|h,(k,v)| h[k.to_s] = v; h}

  json['aps'] ||= {}
  json['aps']['alert'] = @alert if @alert
  json['aps']['badge'] = @badge.to_i rescue 0 if @badge
  json['aps']['sound'] = @sound if @sound
  json['aps']['category'] = @category if @category
  json['aps']['content-available'] = 1 if @content_available

  json
end
sent?() click to toggle source
# File lib/apns_provider_api/notification.rb, line 78
def sent?
  !!@sent_at
end
trim_alert(alert, available_bytes, dottize) click to toggle source
# File lib/apns_provider_api/notification.rb, line 82
def trim_alert(alert, available_bytes, dottize)
  if alert.size > available_bytes
    raise "the notification payload was too large \nTip: use dottize option to auto trim the notification alert" unless dottize
    chars_to_delete = alert.size - available_bytes
    alert =  dottize_no_broken_words(alert, alert.size - chars_to_delete)
  end
  alert
end

Private Instance Methods

dottize_no_broken_words(string, limit=3) click to toggle source
# File lib/apns_provider_api/notification.rb, line 102
def dottize_no_broken_words(string, limit=3)
  size = string.size
  size > limit ? "#{string[0,string[0,limit-3].rindex(" ") || limit-3]}..." : string
end
generate_uuid(uuid=nil) click to toggle source

def error

APNSError.new(@apns_error_code) if @apns_error_code and @apns_error_code.nonzero?

end

# File lib/apns_provider_api/notification.rb, line 97
def generate_uuid(uuid=nil)
  uuid = SecureRandom.uuid unless uuid
  @uuid = uuid
end