Skip to content

Commit

Permalink
Adding the ability to surface http2 error to the app and closing the …
Browse files Browse the repository at this point in the history
…apnotic connection (#503)
  • Loading branch information
avanrielly authored Nov 11, 2024
1 parent 00b5f96 commit 3b9ec9e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/delivery_methods/ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CommentNotifier < ApplicationNotifier
config.key_id = Rails.application.credentials.dig(:ios, :key_id)
config.team_id = Rails.application.credentials.dig(:ios, :team_id)
config.apns_key = Rails.application.credentials.dig(:ios, :apns_key)
config.error_handler = ->(exception) { ... }
end
end
```
Expand Down Expand Up @@ -66,6 +67,9 @@ end

Set this to `true` to use the APNS sandbox environment for sending notifications. This is required when running the app to your device via Xcode. Running the app via TestFlight or the App Store should not use development.

* `error_handler` - *Optional*
A lambda to allow your app to handle Apnotic errors.

## Gathering Notification Tokens

A recipient can have multiple tokens (i.e. multiple iOS devices), so make sure to return them all.
Expand Down Expand Up @@ -106,7 +110,7 @@ class CommentNotifier < ApplicationNotifier
end
```

Another common action is to update the badge after a user reads a notification.
Another common action is to update the badge after a user reads a notification.

This is a great use of the Noticed::Ephemeral class. Since it's all in-memory, it will perform the job and not touch the database.

Expand Down
4 changes: 4 additions & 0 deletions lib/noticed/delivery_methods/ios.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def deliver
connection_pool.with do |connection|
response = connection.push(apn)
raise "Timeout sending iOS push notification" unless response
connection.close

if bad_token?(response) && config[:invalid_token]
# Allow notification to cleanup invalid iOS device tokens
Expand Down Expand Up @@ -62,6 +63,9 @@ def new_connection_pool(development:)
handler = proc do |connection|
connection.on(:error) do |exception|
Rails.logger.info "Apnotic exception raised: #{exception}"
if config[:error_handler].respond_to?(:call)
notification.instance_exec(exception, &config[:error_handler])
end
end
end

Expand Down
3 changes: 3 additions & 0 deletions test/delivery_methods/ios_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def push(apn)
@deliveries.push(apn)
@response
end

def close
end
end

class FakeResponse
Expand Down

0 comments on commit 3b9ec9e

Please sign in to comment.