Class: ApnsSimple::Notification

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

Constant Summary collapse

PAYLOAD_MAX_BYTESIZE =
2048

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, custom_payload = {}) ⇒ Notification

Returns a new instance of Notification.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/apns_simple/notification.rb', line 11

def initialize(options, custom_payload = {})
  token = options.fetch(:token)
  alert = options[:alert]
  badge = options[:badge]
  sound = options[:sound] || 'default'
  content_available = options[:content_available]

  payload_hash = { aps: {} }
  payload_hash[:aps][:alert] = alert if alert
  payload_hash[:aps][:badge] = badge if badge
  payload_hash[:aps][:sound] = sound if sound
  payload_hash[:aps]['content-available'] = 1 if content_available
  payload_hash.merge! custom_payload

  packed_token = [token.gsub(/[<\s>]/,'')].pack('H*')
  packed_message = payload_hash.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
  payload_size = packed_message.bytesize

  if payload_size > PAYLOAD_MAX_BYTESIZE
    self.error = true
    self.error_message = "Payload size is #{payload_size} bytes but maximum #{PAYLOAD_MAX_BYTESIZE} bytes allowed."
  end

  self.payload = [0, 0, 32, packed_token, 0, payload_size, packed_message].pack("ccca*cca*")
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/apns_simple/notification.rb', line 9

def error
  @error
end

#error_codeObject

Returns the value of attribute error_code.



9
10
11
# File 'lib/apns_simple/notification.rb', line 9

def error_code
  @error_code
end

#error_messageObject

Returns the value of attribute error_message.



9
10
11
# File 'lib/apns_simple/notification.rb', line 9

def error_message
  @error_message
end

#payloadObject (readonly)

Returns the value of attribute payload.



8
9
10
# File 'lib/apns_simple/notification.rb', line 8

def payload
  @payload
end