class P8push::Notification

Constants

MAXIMUM_PAYLOAD_SIZE

Attributes

alert[RW]
apns_error_code[W]
badge[RW]
category[RW]
content_available[RW]
custom_data[RW]
device[RW]
device=[RW]
expiry[RW]
id[RW]
media_url[RW]
mutable_content[RW]
priority[RW]
sent_at[R]
sound[RW]
token[RW]
topic[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/p8push/notification.rb, line 39
def initialize(options = {})
  @token = options.delete(:token) || options.delete(:device)
  @alert = options.delete(:alert)
  @media_url = options.delete(:media_url)
  @topic = options.delete(:topic) || ENV['APN_BUNDLE_ID']
  @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)
  @mutable_content = options.delete(:mutable_content)

  @custom_data = options
end

Public Instance Methods

error() click to toggle source
# File lib/p8push/notification.rb, line 96
def error
  APNSError.new(@apns_error_code) if @apns_error_code && @apns_error_code.nonzero?
end
mark_as_sent!() click to toggle source
# File lib/p8push/notification.rb, line 80
def mark_as_sent!
  @sent_at = Time.now
end
mark_as_unsent!() click to toggle source
# File lib/p8push/notification.rb, line 84
def mark_as_unsent!
  @sent_at = nil
end
message() click to toggle source
# File lib/p8push/notification.rb, line 71
def message
  data = [device_token_item,
          payload_item,
          identifier_item,
          expiration_item,
          priority_item].compact.join
  [2, data.bytes.count, data].pack('cNa*')
end
payload() click to toggle source
# File lib/p8push/notification.rb, line 56
def payload
  json = {}.merge(@custom_data || {}).inject({}) { |h, (k, v)| h[k.to_s] = v; h }

  json['aps'] ||= {}
  json['aps']['alert'] = @alert.as_json 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['aps']['mutable-content'] = 1 if @mutable_content
  json['media-url'] = @media_url.present? ? "#{@media_url}" : ""

  json
end
sent?() click to toggle source
# File lib/p8push/notification.rb, line 88
def sent?
  !!@sent_at
end
valid?() click to toggle source
# File lib/p8push/notification.rb, line 92
def valid?
  payload.to_json.bytesize <= MAXIMUM_PAYLOAD_SIZE
end

Private Instance Methods

device_token_item() click to toggle source
# File lib/p8push/notification.rb, line 102
def device_token_item
  [1, 32, @token.gsub(/[<\s>]/, '')].pack('cnH64')
end
expiration_item() click to toggle source
# File lib/p8push/notification.rb, line 115
def expiration_item
  [4, 4, @expiry.to_i].pack('cnN') unless @expiry.nil?
end
identifier_item() click to toggle source
# File lib/p8push/notification.rb, line 111
def identifier_item
  [3, 4, @id].pack('cnN') unless @id.nil?
end
payload_item() click to toggle source
# File lib/p8push/notification.rb, line 106
def payload_item
  json = payload.to_json
  [2, json.bytes.count, json].pack('cna*')
end
priority_item() click to toggle source
# File lib/p8push/notification.rb, line 119
def priority_item
  [5, 1, @priority].pack('cnc') unless @priority.nil?
end