Class: ApnsSimple::Notification

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Notification.



9
10
11
12
13
14
15
16
# File 'lib/apns_simple/notification.rb', line 9

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

Instance Attribute Details

#alertObject (readonly)

Returns the value of attribute alert.



6
7
8
# File 'lib/apns_simple/notification.rb', line 6

def alert
  @alert
end

#badgeObject (readonly)

Returns the value of attribute badge.



6
7
8
# File 'lib/apns_simple/notification.rb', line 6

def badge
  @badge
end

#content_availableObject (readonly)

Returns the value of attribute content_available.



6
7
8
# File 'lib/apns_simple/notification.rb', line 6

def content_available
  @content_available
end

#custom_payloadObject (readonly)

Returns the value of attribute custom_payload.



6
7
8
# File 'lib/apns_simple/notification.rb', line 6

def custom_payload
  @custom_payload
end

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#error_codeObject

Returns the value of attribute error_code.



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

def error_code
  @error_code
end

#error_messageObject

Returns the value of attribute error_message.



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

def error_message
  @error_message
end

#soundObject (readonly)

Returns the value of attribute sound.



6
7
8
# File 'lib/apns_simple/notification.rb', line 6

def sound
  @sound
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/apns_simple/notification.rb', line 6

def token
  @token
end

Instance Method Details

#payloadObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/apns_simple/notification.rb', line 18

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

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