Class: LeadZeppelin::APNS::Notification

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_token, message, opts = {}) ⇒ Notification

Returns a new instance of Notification.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lead_zeppelin/apns/notification.rb', line 6

def initialize(device_token, message, opts={})
  @device_token = device_token
  @opts = opts
  
  @identifier = @opts[:identifier] || SecureRandom.random_bytes(4)
  @identifier = @identifier.to_s

  @expiry = @opts[:expiry].nil? ? 1 : @opts[:expiry].to_i

  if opts[:raw] == true
    @message = message
  elsif message.is_a?(Hash)
    other = message.delete(:other)
    @message = {aps: message}
    @message.merge!(other) if other
  elsif message.is_a?(String)
    @message = {aps: {alert: message}}
  else
    raise ArgumentError, "notification message must be hash or string"
  end
end

Instance Attribute Details

#device_tokenObject (readonly)

Returns the value of attribute device_token.



4
5
6
# File 'lib/lead_zeppelin/apns/notification.rb', line 4

def device_token
  @device_token
end

#expiryObject (readonly)

Returns the value of attribute expiry.



4
5
6
# File 'lib/lead_zeppelin/apns/notification.rb', line 4

def expiry
  @expiry
end

#identifierObject (readonly)

Returns the value of attribute identifier.



4
5
6
# File 'lib/lead_zeppelin/apns/notification.rb', line 4

def identifier
  @identifier
end

Instance Method Details

#message_jsonObject



36
37
38
39
# File 'lib/lead_zeppelin/apns/notification.rb', line 36

def message_json
  return @message if @opts[:raw] == true
  @message_json ||= MultiJson.encode @message
end

#packaged_tokenObject



32
33
34
# File 'lib/lead_zeppelin/apns/notification.rb', line 32

def packaged_token
  [device_token.gsub(/[\s|<|>]/,'')].pack('H*')
end

#payloadObject



28
29
30
# File 'lib/lead_zeppelin/apns/notification.rb', line 28

def payload
  [1, @identifier, @expiry, 0, 32, packaged_token, 0, message_json.bytesize, message_json].pack("cA4Ncca*cca*")
end