Class: ApnServer::Notification
- Inherits:
-
Object
- Object
- ApnServer::Notification
- Includes:
- Payload
- Defined in:
- lib/apnserver/notification.rb
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#custom ⇒ Object
Returns the value of attribute custom.
-
#device_token ⇒ Object
Returns the value of attribute device_token.
-
#sound ⇒ Object
Returns the value of attribute sound.
Class Method Summary collapse
Instance Method Summary collapse
Methods included from Payload
#create_payload, #create_payload_from_hash
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
18 19 20 |
# File 'lib/apnserver/notification.rb', line 18 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
18 19 20 |
# File 'lib/apnserver/notification.rb', line 18 def badge @badge end |
#custom ⇒ Object
Returns the value of attribute custom.
18 19 20 |
# File 'lib/apnserver/notification.rb', line 18 def custom @custom end |
#device_token ⇒ Object
Returns the value of attribute device_token.
18 19 20 |
# File 'lib/apnserver/notification.rb', line 18 def device_token @device_token end |
#sound ⇒ Object
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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/apnserver/notification.rb', line 66 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 = JSON.parse(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
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/apnserver/notification.rb', line 53 def self.valid?(p) begin Notification.parse(p) rescue PayloadInvalid => p puts "PayloadInvalid: #{p}" false rescue JSON::ParserError => p false rescue RuntimeError false end end |
Instance Method Details
#json_payload ⇒ Object
29 30 31 32 33 |
# File 'lib/apnserver/notification.rb', line 29 def json_payload j = defined?(Rails) ? payload.to_json : JSON.generate(payload) raise PayloadInvalid.new("The payload is larger than allowed: #{j.length}") if j.size > 256 j end |
#payload ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/apnserver/notification.rb', line 21 def payload p = Hash.new [:badge, :alert, :sound, :custom].each do |k| p[k] = send(k) if send(k) end create_payload(p) end |
#push ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/apnserver/notification.rb', line 35 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_bytes ⇒ Object
48 49 50 51 |
# File 'lib/apnserver/notification.rb', line 48 def to_bytes j = json_payload [0, 0, device_token.size, device_token, 0, j.size, j].pack("ccca*cca*") end |