Class: Pling::APN::Gateway
Overview
Pling gateway to communicate with Apple’s Push Notification service.
This gateway handles these device types:
:apple, :apn, :ios, :ipad, :iphone, :ipod
Configure it by providing the path to your certificate:
Pling::APN::Gateway.new({
:certificate => '/path/to/certificate.pem', # Required
:host => 'gateway.sandbox.push.apple.com' # Optional
})
Instance Method Summary collapse
-
#deliver!(message, device) ⇒ Object
Sends the given message to the given device without using the middleware.
-
#initialize(configuration) ⇒ Gateway
constructor
Initializes a new gateway to Apple’s Push Notification service.
-
#setup! ⇒ Object
Establishes a new connection if connection is not available or closed.
Methods inherited from Gateway
#deliver, discover, handled_types, handles, #handles?, #teardown!
Constructor Details
#initialize(configuration) ⇒ Gateway
Initializes a new gateway to Apple’s Push Notification service
31 32 33 34 35 |
# File 'lib/pling/apn/gateway.rb', line 31 def initialize(configuration) super require_configuration(:certificate) setup! end |
Instance Method Details
#deliver!(message, device) ⇒ Object
Sends the given message to the given device without using the middleware.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pling/apn/gateway.rb', line 48 def deliver!(, device) data = { :aps => { :alert => .body, :badge => .badge && .badge.to_i, :sound => .sound, }.delete_if { |_, value| value.nil? } } data[:aps]['content-available'] = 1 if .content_available data[:aps]['category'] = .category if .category data.merge!(.payload) if configuration[:payload] && .payload data = data.to_json if data.bytesize > 2048 raise Pling::DeliveryFailed.new( "Payload size of #{data.bytesize} exceeds allowed size of 2048 bytes.", , device) end token = [device.identifier].pack('H*') connection.write([0, token.bytesize, token, data.bytesize, data].pack('cna*na*')) end |
#setup! ⇒ Object
Establishes a new connection if connection is not available or closed
39 40 41 |
# File 'lib/pling/apn/gateway.rb', line 39 def setup! setup_connection_pool end |