Class: Push0r::ApnsService

Inherits:
Service
  • Object
show all
Defined in:
lib/push0r/APNS/ApnsService.rb

Overview

ApnsService is a Service implementation to push notifications to iOS and OSX users using the Apple Push Notification Service.

Examples:

queue = Push0r::Queue.new

apns_service = Push0r::ApnsService.new(File.read("aps.pem"), Push0r::ApnsEnvironment::SANDBOX)
queue.register_service(apns_service)

Instance Method Summary collapse

Constructor Details

#initialize(certificate_data, environment = ApnsEnvironment::PRODUCTION) ⇒ ApnsService

Returns a new ApnsService instance

Parameters:

  • certificate_data (String)

    the Apple push certificate in PEM format

  • environment (Fixnum) (defaults to: ApnsEnvironment::PRODUCTION)

    the environment to use when sending messages. Either ApnsEnvironment::PRODUCTION or ApnsEnvironment::SANDBOX. Defaults to ApnsEnvironment::PRODUCTION.



33
34
35
36
37
38
39
# File 'lib/push0r/APNS/ApnsService.rb', line 33

def initialize(certificate_data, environment = ApnsEnvironment::PRODUCTION)
  @certificate_data = certificate_data
  @environment = environment
  @ssl = nil
  @sock = nil
  @messages = []
end

Instance Method Details

#can_send?(message) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



42
43
44
# File 'lib/push0r/APNS/ApnsService.rb', line 42

def can_send?(message)
  return message.is_a?(ApnsPushMessage) && message.environment == @environment
end

#end_pushObject

See Also:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/push0r/APNS/ApnsService.rb', line 57

def end_push
  failed_messages = []
  result = false
  begin
    begin
      setup_ssl(true)
    rescue SocketError => e
      puts "Error: #{e}"
      break
    end
    (result, error_message, error_code) = transmit_messages
    unless result
      failed_messages << FailedMessage.new(error_code, [error_message.receiver_token], error_message)
      reset_message(error_message.identifier)
      result = true if @messages.empty?
    end
  end until result

  close_ssl

  @messages = [] ## reset
  return [failed_messages, []]
end

#get_feedbackArray<String>

Calls the APNS feedback service and returns an array of expired push tokens

Returns:

  • (Array<String>)

    an array of expired push tokens



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/push0r/APNS/ApnsService.rb', line 83

def get_feedback
  tokens = []

  begin
    setup_ssl(true)
  rescue SocketError => e
    puts "Error: #{e}"
    return tokens
  end

  if IO.select([@ssl], nil, nil, 1)
    while (line = @ssl.read(38))
      f = line.unpack('N1n1H64')
      time = Time.at(f[0])
      token = f[2].scan(/.{8}/).join(' ')
      tokens << token
    end
  end

  close_ssl

  return tokens
end

#init_pushObject

See Also:



52
53
54
# File 'lib/push0r/APNS/ApnsService.rb', line 52

def init_push
  # not used for apns
end

#send(message) ⇒ Object

See Also:



47
48
49
# File 'lib/push0r/APNS/ApnsService.rb', line 47

def send(message)
  @messages << message
end