Class: APND::Feedback

Inherits:
Object
  • Object
show all
Defined in:
lib/apnd/feedback.rb

Overview

APND::Feedback receives feedback from Apple when notifications are being rejected for a specific token. This is usually due to the user uninstalling your application.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFeedback

Create a connection to Apple’s Feedback Service



41
42
43
44
45
46
47
48
# File 'lib/apnd/feedback.rb', line 41

def initialize
  @apple = APND::Daemon::AppleConnection.new({
    :cert      => APND.settings.apple.cert,
    :cert_pass => APND.settings.apple.cert_pass,
    :host      => self.class.upstream_host,
    :port      => self.class.upstream_port.to_i
  })
end

Class Attribute Details

.upstream_hostObject

The host to receive feedback from, usually apple



13
14
15
# File 'lib/apnd/feedback.rb', line 13

def upstream_host
  @upstream_host
end

.upstream_portObject

The port to connect to upstream_host on



18
19
20
# File 'lib/apnd/feedback.rb', line 18

def upstream_port
  @upstream_port
end

Class Method Details

.find_stale_devices(&block) ⇒ Object

Connect to Apple’s Feedback Service and return an array of device tokens that should no longer receive push notifications



31
32
33
34
35
36
# File 'lib/apnd/feedback.rb', line 31

def self.find_stale_devices(&block)
  feedback = self.new
  devices  = feedback.receive
  devices.each { |device| yield *device } if block_given?
  devices
end

Instance Method Details

#receiveObject

Receive feedback from Apple and return an array of device tokens



53
54
55
56
57
58
59
60
61
62
# File 'lib/apnd/feedback.rb', line 53

def receive
  tokens = []
  @apple.open do |sock|
    while line = sock.gets
      payload = line.strip.unpack('N1n1H140')
      tokens << [payload[2].strip, Time.at(payload[0])]
    end
  end
  tokens
end