class P8Pusher::Client
Attributes
jwt_uri[RW]
Public Class Methods
development()
click to toggle source
# File lib/p8_pusher/client.rb, line 13 def development client = new client.jwt_uri = APPLE_DEVELOPMENT_JWT_URI client end
new()
click to toggle source
# File lib/p8_pusher/client.rb, line 26 def initialize @private_key = File.read(ENV['APN_PRIVATE_KEY']) @team_id = ENV['APN_TEAM_ID'] @key_id = ENV['APN_KEY_ID'] @timeout = Float(ENV['APN_TIMEOUT'] || 2.0) rescue StandardError => e raise "#{e.message}: invalid keys, make sure you have the required keys in .env file" end
production()
click to toggle source
# File lib/p8_pusher/client.rb, line 19 def production client = new client.jwt_uri = APPLE_PRODUCTION_JWT_URI client end
Public Instance Methods
jwt_http2_post(topic, payload, token)
click to toggle source
# File lib/p8_pusher/client.rb, line 35 def jwt_http2_post(topic, payload, token) ec_key = OpenSSL::PKey::EC.new(@private_key) jwt_token = JWT.encode({iss: @team_id, iat: Time.now.to_i}, ec_key, 'ES256', {kid: @key_id}) client = NetHttp2::Client.new(@jwt_uri) h = {} h['content-type'] = 'application/json' h['apns-expiration'] = '0' h['apns-priority'] = '10' h['apns-topic'] = topic h['authorization'] = "bearer #{jwt_token}" res = client.call(:post, '/3/device/'+token, body: payload.to_json, timeout: @timeout, headers: h) client.close return nil if res.status.to_i == 200 res.body end
push(*notifications)
click to toggle source
# File lib/p8_pusher/client.rb, line 57 def push(*notifications) return if notifications.empty? notifications.flatten! notifications.each_with_index do |notification, index| next unless notification.kind_of?(Notification) next if notification.sent? next unless notification.valid? notification.id = index err = jwt_http2_post(notification.topic, notification.payload, notification.token) if err.nil? notification.mark_as_sent! else puts err notification.apns_error_code = err notification.mark_as_unsent! end end end