Skip to content

Commit

Permalink
Separate connection pools for iOS deliveries to support sandbox and p…
Browse files Browse the repository at this point in the history
…roduction devices in the same app
  • Loading branch information
excid3 committed Jan 15, 2024
1 parent 797878f commit 1dacd09
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
13 changes: 13 additions & 0 deletions docs/delivery_methods/ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
19 changes: 10 additions & 9 deletions lib/noticed/delivery_methods/ios.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions test/delivery_methods/ios_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 1dacd09

Please sign in to comment.