Class: ApnServer::Notification

Inherits:
Object
  • Object
show all
Includes:
Payload
Defined in:
lib/apnserver/notification.rb

Constant Summary

Constants included from Payload

Payload::PayloadInvalid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Payload

#create_payload, #create_payload_from_hash

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



18
19
20
# File 'lib/apnserver/notification.rb', line 18

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



18
19
20
# File 'lib/apnserver/notification.rb', line 18

def badge
  @badge
end

#customObject

Returns the value of attribute custom.



18
19
20
# File 'lib/apnserver/notification.rb', line 18

def custom
  @custom
end

#device_tokenObject

Returns the value of attribute device_token.



18
19
20
# File 'lib/apnserver/notification.rb', line 18

def device_token
  @device_token
end

#soundObject

Returns the value of attribute sound.



18
19
20
# File 'lib/apnserver/notification.rb', line 18

def sound
  @sound
end

Class Method Details

.parse(p) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/apnserver/notification.rb', line 65

def self.parse(p)
  buffer = p.dup
  notification = Notification.new

  header = buffer.slice!(0, 3).unpack('ccc')
  if header[0] != 0
    raise RuntimeError.new("Header of notification is invalid: #{header.inspect}")
  end

  # parse token
  notification.device_token = buffer.slice!(0, 32).unpack('a*').first

  # parse json payload
  payload_len = buffer.slice!(0, 2).unpack('CC')
  j = buffer.slice!(0, payload_len.last)
  result = ActiveSupport::JSON.decode(j)

  ['alert', 'badge', 'sound'].each do |k|
    notification.send("#{k}=", result['aps'][k]) if result['aps'] && result['aps'][k]
  end
  result.delete('aps')
  notification.custom = result

  notification
end

.valid?(p) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/apnserver/notification.rb', line 52

def self.valid?(p)
  begin
    Notification.parse(p)
  rescue PayloadInvalid => p
    Config.logger.error "PayloadInvalid: #{p}"
    false
  rescue RuntimeError
    false
  rescue Exception => e
    false
  end
end

Instance Method Details

#json_payloadObject

Raises:



28
29
30
31
32
# File 'lib/apnserver/notification.rb', line 28

def json_payload
  j = ActiveSupport::JSON.encode(payload)
  raise PayloadInvalid.new("The payload is larger than allowed: #{j.length}") if j.size > 256
  j
end

#payloadObject



20
21
22
23
24
25
26
# File 'lib/apnserver/notification.rb', line 20

def payload
  p = Hash.new
  [:badge, :alert, :sound, :custom].each do |k|
    p[k] = send(k) if send(k)
  end
  create_payload(p)
end

#pushObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/apnserver/notification.rb', line 34

def push
  if Config.pem.nil?
    socket = TCPSocket.new(Config.host || 'localhost', Config.port.to_i || 22195)
    socket.write(to_bytes)
    socket.close
  else
    client = ApnServer::Client.new(Config.pem, Config.host || 'gateway.push.apple.com', Config.port.to_i || 2195)
    client.connect!
    client.write(self)
    client.disconnect!
  end
end

#to_bytesObject



47
48
49
50
# File 'lib/apnserver/notification.rb', line 47

def to_bytes
  j = json_payload
  [0, 0, device_token.size, device_token, 0, j.size, j].pack("ccca*cca*")
end