Class: Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/apn/payload.rb

Instance Method Summary collapse

Constructor Details

#initialize(notification_hash, max_bytes) ⇒ Payload

Returns a new instance of Payload.



3
4
5
6
# File 'lib/apn/payload.rb', line 3

def initialize(notification_hash, max_bytes)
  @notification_hash = notification_hash
  @max_bytes = max_bytes
end

Instance Method Details

#packageObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/apn/payload.rb', line 8

def package
  str = encode(@notification_hash)

  if APN.truncate_alert && str.bytesize > @max_bytes
    max_bytesize = @max_bytes - (str.bytesize - alert.bytesize)

    if max_bytesize <= 0
      escaped_max_bytesize = @max_bytes - (str.bytesize - encode(alert).bytesize)
      raise "Even truncating the alert won't be enough to have a #{@max_bytes} message" if escaped_max_bytesize <= 0
      truncate_escaped!(escaped_max_bytesize)
    else
      truncate_alert!(max_bytesize)
    end
    str = encode(@notification_hash)
  end
  str
end