lib/apnserver/notification.rb in bpoweski-apnserver-0.0.9 vs lib/apnserver/notification.rb in bpoweski-apnserver-0.0.10
- old
+ new
@@ -1,15 +1,23 @@
require 'apnserver/payload'
require 'json'
module ApnServer
+ class Config
+ class << self
+ attr_accessor :host, :port, :pem, :password
+ end
+ end
+
+
class Notification
include ApnServer::Payload
attr_accessor :device_token, :alert, :badge, :sound, :custom
+
def payload
p = Hash.new
[:badge, :alert, :sound, :custom].each do |k|
p[k] = send(k) if send(k)
end
@@ -18,9 +26,20 @@
def json_payload
p = JSON.generate(payload)
raise PayloadInvalid.new("Payload of #{p.size} is longer than 256") if p.size > 256
p
+ end
+
+ def push
+ if Config.pem.nil?
+ socket = TCPSocket.new(Config.host || 'localhost', Config.port.to_i || 22195)
+ socket.write(notification.to_bytes)
+ else
+ client = ApnServer::Client.new(Config.pem, Config.host || 'gateway.push.apple.com', Config.port.to_i || 2195)
+ client.connect!
+ client.write(notification)
+ end
end
def to_bytes
json = json_payload
[0, 0, device_token.size, device_token, 0, json.size, json].pack("ccca*cca*")