Class: Pling::APN::Gateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/pling/apn/gateway.rb

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)

Methods inherited from Gateway

#deliver, discover, handled_types, handles, #handles?, #teardown!

Constructor Details

- (Gateway) initialize(configuration)

Initializes a new gateway to Apple's Push Notification service

Parameters:

  • configuration (Hash)

Options Hash (configuration):

  • :certificate (#to_s)

    Path to PEM certificate file (Required)

  • :host (String)

    Host to connect to (Default: gateway.push.apple.com)

  • :port (Integer)

    Port to connect to (Default: 2195)



30
31
32
33
34
# File 'lib/pling/apn/gateway.rb', line 30

def initialize(configuration)
  super
  require_configuration(:certificate)
  setup!
end

Instance Method Details

- (Object) deliver!(message, device)

Sends the given message to the given device without using the middleware.

Parameters:

  • message (#to_pling_message)
  • device (#to_pling_device)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pling/apn/gateway.rb', line 47

def deliver!(message, device)
  data = {
    :aps => {
      :alert => message.body,
      :badge => message.badge && message.badge.to_i,
      :sound => message.sound
    }.delete_if { |_, value| value.nil? }
  }

  data.merge!(message.payload) if configuration[:payload] && message.payload

  data = data.to_json

  if data.bytesize > 256
    raise Pling::DeliveryFailed.new(
      "Payload size of #{data.bytesize} exceeds allowed size of 256 bytes.",
      message,
    device)
  end

  token = [device.identifier].pack('H*')

  connection.write([0, token.bytesize, token, data.bytesize, data].pack('cna*na*'))
end

- (Object) setup!

Establishes a new connection if connection is not available or closed



38
39
40
# File 'lib/pling/apn/gateway.rb', line 38

def setup!
  connection.reopen if connection.closed?
end