Skip to content

Commit

Permalink
Adding :full_cert_path, which allows explicitly specifying custom cer…
Browse files Browse the repository at this point in the history
…tificate filenames
  • Loading branch information
kdonovan committed May 15, 2011
1 parent 9599491 commit 4df180a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
8 changes: 5 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ For production, you're probably better off running a dedicated daemon and settin
# To run daemon. Pass --help to print all options
./script/apn_sender --environment=production --verbose start

Note the --environment must be explicitly set (separately from your <code>RAILS_ENV</code>) to production in order to send messages via the production APN servers. Any other environment sends messages through Apple's sandbox servers at <code>gateway.sandbox.push.apple.com</code>.
Note the --environment must be explicitly set (separately from your <code>Rails.env</code>) to production in order to send messages via the production APN servers. Any other environment sends messages through Apple's sandbox servers at <code>gateway.sandbox.push.apple.com</code>.

Check <code>RAILS_ROOT/logs/apn_sender.log</code> for debugging output. In addition to logging any major errors there, apn_sender hooks into the Resque::Worker logging to display any verbose or very_verbose worker output in apn_sender.log file as well.
Also, there are two similar options <code>:cert_path</code> and <code>:full_cert_path</code>. The former specifies the directory in which to file the .pem file (either apn_production.pem or apn_development.pem, depending on the environment). The latter specifies a .pem file explicitly, allowing customized certificate names for those that need them.

Check <code>logs/apn_sender.log</code> for debugging output. In addition to logging any major errors there, apn_sender hooks into the Resque::Worker logging to display any verbose or very_verbose worker output in apn_sender.log file as well.


=== 3. Checking Apple's Feedback Service
Expand All @@ -60,7 +62,7 @@ Since push notifications are a fire-and-forget sorta deal, where you get no indi

It's actually really simple - you connect to them periodically and they give you a big dump of tokens you shouldn't send to anymore. The gem wraps this up nicely -- just call:

# APN::Feedback accepts the same optional :environment and :cert_path options as APN::Sender
# APN::Feedback accepts the same optional :environment and :cert_path / :full_cert_path options as APN::Sender
feedback = APN::Feedback.new()

tokens = feedback.tokens # => Array of device tokens
Expand Down
22 changes: 12 additions & 10 deletions lib/apn/connection/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,20 @@ def apn_production?

# Get a fix on the .pem certificate we'll be using for SSL
def setup_paths
# Set option defaults. RAILS_ROOT is still here not from Rails, but to handle passing in root from sender_daemon
@opts[:root_path] ||= defined?(::Rails.root) ? ::Rails.root.to_s : (defined?(RAILS_ROOT) ? RAILS_ROOT : '/')
@opts[:cert_path] ||= File.join(File.expand_path(@opts[:root_path]), "config", "certs")
@opts[:environment] ||= ::Rails.env if defined?(::Rails.env)

log_and_die("Missing certificate path. Please specify :cert_path when initializing class.") unless @opts[:cert_path]

cert_name = apn_production? ? "apn_production.pem" : "apn_development.pem"
cert_path = File.join(@opts[:cert_path], cert_name)

@apn_cert = File.exists?(cert_path) ? File.read(cert_path) : nil
log_and_die("Missing apple push notification certificate in #{cert_path}") unless @apn_cert
# Accept a complete :full_cert_path allowing arbitrary certificate names, or create a default from the Rails env
cert_path = @opts[:full_cert_path] || begin
# Note that RAILS_ROOT is still here not from Rails, but to handle passing in root from sender_daemon
@opts[:root_path] ||= defined?(::Rails.root) ? ::Rails.root.to_s : (defined?(RAILS_ROOT) ? RAILS_ROOT : '/')
@opts[:cert_path] ||= File.join(File.expand_path(@opts[:root_path]), "config", "certs")
@opts[:cert_name] ||= apn_production? ? "apn_production.pem" : "apn_development.pem"

File.join(@opts[:cert_path], @opts[:cert_name])
end

@apn_cert = File.read(cert_path) if File.exists?(cert_path)
log_and_die("Please specify correct :full_cert_path. No apple push notification certificate found in: #{cert_path}") unless @apn_cert
end

# Open socket to Apple's servers
Expand Down
3 changes: 3 additions & 0 deletions lib/apn/sender_daemon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def initialize(args)
opts.on('--cert-path=NAME', 'Path to directory containing apn .pem certificates.') do |path|
@options[:cert_path] = path
end
opts.on('c', '--full-cert-path=NAME', 'Full path to desired .pem certificate (overrides environment selector).') do |path|
@options[:full_cert_path] = path
end
opts.on('-n', '--number-of-workers=WORKERS', "Number of unique workers to spawn") do |worker_count|
@options[:worker_count] = worker_count.to_i rescue 1
end
Expand Down
2 changes: 1 addition & 1 deletion lib/apn/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
worker = nil

begin
worker = APN::Sender.new(:cert_path => ENV['CERT_PATH'], :environment => ENV['ENVIRONMENT'])
worker = APN::Sender.new(:full_cert_path => ENV['FULL_CERT_PATH'], :cert_path => ENV['CERT_PATH'], :environment => ENV['ENVIRONMENT'])
worker.verbose = ENV['LOGGING'] || ENV['VERBOSE']
worker.very_verbose = ENV['VVERBOSE']
rescue Exception => e
Expand Down

0 comments on commit 4df180a

Please sign in to comment.