diff --git a/docs/delivery_methods/ios.md b/docs/delivery_methods/ios.md index 48b32885..93047b3c 100644 --- a/docs/delivery_methods/ios.md +++ b/docs/delivery_methods/ios.md @@ -122,3 +122,16 @@ class CommentNotification end end ``` + +## Delivering to Sandboxes and real devices + +```ruby +deliver_by :ios do |config| + config.device_tokens = ->(recipient) { recipient.notification_tokens.where(environment: :production, platform: :iOS).pluck(:token) } +end + +deliver_by :ios_development do |config| + config.development = true + config.device_tokens = ->(recipient) { recipient.notification_tokens.where(environment: :development, platform: :iOS).pluck(:token) } +end +``` diff --git a/lib/noticed/delivery_methods/ios.rb b/lib/noticed/delivery_methods/ios.rb index 70d3e47d..d58e67ca 100644 --- a/lib/noticed/delivery_methods/ios.rb +++ b/lib/noticed/delivery_methods/ios.rb @@ -3,7 +3,7 @@ module Noticed module DeliveryMethods class Ios < DeliveryMethod - cattr_accessor :connection_pool + cattr_accessor :development_connection_pool, :production_connection_pool required_options :bundle_identifier, :key_id, :team_id, :apns_key, :device_tokens @@ -12,6 +12,7 @@ def deliver apn = Apnotic::Notification.new(device_token) format_notification(apn) + connection_pool = !!evaluate_option(:development) ? development_pool : production_pool connection_pool.with do |connection| response = connection.push(apn) raise "Timeout sending iOS push notification" unless response @@ -44,18 +45,22 @@ def bad_token?(response) response.status == "410" || (response.status == "400" && response.body["reason"] == "BadDeviceToken") end - def connection_pool - self.class.connection_pool ||= new_connection_pool + def development_pool + self.class.development_connection_pool ||= new_connection_pool(development: true) end - def new_connection_pool + def production_pool + self.class.production_connection_pool ||= new_connection_pool(development: false) + end + + def new_connection_pool(development:) handler = proc do |connection| connection.on(:error) do |exception| Rails.logger.info "Apnotic exception raised: #{exception}" end end - if development? + if development Apnotic::ConnectionPool.development(connection_pool_options, pool_options, &handler) else Apnotic::ConnectionPool.new(connection_pool_options, pool_options, &handler) @@ -71,10 +76,6 @@ def connection_pool_options } end - def development? - !!evaluate_option(:development) - end - def pool_options {size: evaluate_option(:pool_size) || 5} end diff --git a/test/delivery_methods/ios_test.rb b/test/delivery_methods/ios_test.rb index 506e6b0f..34927364 100644 --- a/test/delivery_methods/ios_test.rb +++ b/test/delivery_methods/ios_test.rb @@ -55,7 +55,7 @@ def ok? test "notifies each device token" do connection_pool = FakeConnectionPool.new(FakeResponse.new("200")) - @delivery_method.stub(:connection_pool, connection_pool) do + @delivery_method.stub(:production_pool, connection_pool) do @delivery_method.deliver end @@ -65,13 +65,12 @@ def ok? test "notifies of invalid tokens for cleanup" do connection_pool = FakeConnectionPool.new(FakeResponse.new("410")) - @delivery_method.stub(:connection_pool, connection_pool) do + @delivery_method.stub(:production_pool, connection_pool) do @delivery_method.deliver end # Our fake connection pool doesn't understand these wouldn't be delivered in the real world assert_equal 2, connection_pool.deliveries.count - assert_equal 2, FakeConnectionPool.invalid_tokens.count end