Class: MobileNotify::Apns::Notification

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

Constant Summary collapse

MAX_PAYLOAD_LENGTH =
256

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_token, payload) ⇒ Notification

Creates new notification with given token and payload APNs4r::Notification.new ‘e754dXXXX…’, { :aps => => “Hey, dude!”, :badge => 1, :custom_data => “asd” }

Parameters:

  • token (String, Fixnum)

    APNs token of device to notify

  • payload (Hash, String)

    attached payload



22
23
24
25
# File 'lib/mobile_notify/apns/notification.rb', line 22

def initialize(device_token, payload)
  @device_token = device_token.delete(' ')
  @payload = payload.kind_of?(Hash) ? payload.to_json : payload
end

Class Method Details

.parse(bitstring) ⇒ Notification

Counterpart of Notification#to_s - parses from binary string

Parameters:

  • bitstring (String)

    string to parse

Returns:



12
13
14
15
# File 'lib/mobile_notify/apns/notification.rb', line 12

def self.parse(bitstring)
  command, tokenlen, device_token, payloadlen, payload = bitstring.unpack("CnH64na*")
  new(device_token, payload)
end

Instance Method Details

#to_dataString

Converts to binary string wich can be writen directly into socket

Returns:

  • (String)

    binary string representation



33
34
35
# File 'lib/mobile_notify/apns/notification.rb', line 33

def to_data
  [0, 32, @device_token, @payload.length, @payload ].pack("CnH*na*")
end

#valid?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/mobile_notify/apns/notification.rb', line 27

def valid?
  @payload.length <= MAX_PAYLOAD_LENGTH
end