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

#initialize(configuration) ⇒ Gateway

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)



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.

Parameters:

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


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!(message, device)
  data = {
    :aps => {
      :alert => message.body,
      :badge => message.badge && message.badge.to_i,
      :sound => message.sound,
    }.delete_if { |_, value| value.nil? }
  }

  data[:aps]['content-available'] = 1 if message.content_available
  data[:aps]['category'] = message.category if message.category
  data.merge!(message.payload) if configuration[:payload] && message.payload

  data = data.to_json

  if data.bytesize > 2048
    raise Pling::DeliveryFailed.new(
      "Payload size of #{data.bytesize} exceeds allowed size of 2048 bytes.",
      message,
    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