Class: ApnServer::Notification
- Inherits:
-
Object
- Object
- ApnServer::Notification
- Includes:
- Payload
- Defined in:
- lib/apnserver/notification.rb
Constant Summary
Constants included from Payload
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
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
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_payload ⇒ Object
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 |
#payload ⇒ Object
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 |
#push ⇒ Object
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_bytes ⇒ Object
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 |