Skip to content

Commit

Permalink
Merge branch 'main' of github.com:excid3/noticed
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Jan 16, 2024
2 parents f63ced2 + e351b9b commit 1ff4932
Showing 1 changed file with 22 additions and 55 deletions.
77 changes: 22 additions & 55 deletions docs/delivery_methods/ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,78 +20,51 @@ https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_

```ruby
class CommentNotification
deliver_by :ios
end
```

With custom configuration:

```ruby
class CommentNotification
deliver_by :ios, format: :ios_format, cert_path: :ios_cert_path, key_id: :ios_key_id, team_id: :ios_team_id, pool_size: 5

# Customize notification
# See https://github.com/ostinelli/apnotic#apnoticnotification
def ios_format(apn)
apn.alert = "Hello world"
apn.custom_payload = { url: root_url }
end

def ios_cert_path
Rails.root.join("config/certs/ios/apns.p8")
end

def ios_key_id
Rails.application.credentials.dig(:ios, :key_id)
end

def ios_team_id
Rails.application.credentials.dig(:ios, :team_id)
deliver_by :ios do |config|
config.device_tokens = ->(recipient) { recipient.notification_tokens.where(platform: :iOS).pluck(:token) }
config.format = ->(apn) {
apn.alert = "Hello world"
apn.custom_payload = {url: root_url(host: "example.org")}
}
config.bundle_identifier = Rails.application.credentials.dig(:ios, :bundle_id)
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)
end
end
```

## Options

* `format: :ios_format` - *Optional*
* `format`

Customize the Apnotic notification object

See https://github.com/ostinelli/apnotic#apnoticnotification

* `bundle_identifier: Rails.application.credentials.dig(:ios, :bundle_identifier)` - *Optional*
* `bundle_identifier`

The APN bundle identifier

* `cert_path: Rails.root.join("config/certs/ios/apns.p8")` - *Optional*
* `apns_key`

The location of your APNs p8 certificate.
This can also accept a StringIO object `StringIO.new("p8 file content as string")`.
As well as a File object `File.open("path/to/p8.file")`.
The contents of your p8 apns key file.

* `key_id: Rails.application.credentials.dig(:ios, :key_id)` - *Optional*
* `key_id`

Your APN Key ID

If nothing passed, we'll default to `Rails.application.credentials.dig(:ios, :key_id)`
If a String is passed, we'll use that as the key ID.
If a Symbol is passed, we'll call the matching method and you can return the Key ID.

* `team_id: Rails.application.credentials.dig(:ios, :team_id)` - *Optional*
* `team_id`

Your APN Team ID

If nothing passed, we'll default to `Rails.application.credentials.dig(:ios, :team_id)`
If a String is passed, we'll use that as the team ID.
If a Symbol is passed, we'll call the matching method and you can return the team ID.

* `pool_size: 5` - *Optional*

The connection pool size for Apnotic

* `development: false` - *Optional*
* `development` - *Optional*

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.
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.

## Gathering Notification Tokens

Expand All @@ -100,8 +73,8 @@ A recipient can have multiple tokens (i.e. multiple iOS devices), so make sure t
Here, the recipient `has_many :notification_tokens` with columns `platform` and `token`.

```ruby
def ios_device_tokens(recipient)
recipient.notification_tokens.where(platform: "iOS").pluck(:token)
deliver_by :ios do |config|
config.device_tokens = ->(recipient) { recipient.notification_tokens.where(platform: :iOS).pluck(:token) }
end
```

Expand All @@ -111,14 +84,8 @@ Apple Push Notifications may fail delivery if the user has removed the app from

```ruby
class CommentNotification
deliver_by :ios

# Remove invalid device tokens
#
# token - the device token from iOS or Android
# platform - "iOS" or "Android"
def cleanup_device_token(token:, platform:)
NotificationToken.where(token: token, platform: platform).destroy_all
deliver_by :ios do |config|
config.invalid_token = ->(token) { NotificationToken.where(token: token).destroy_all }
end
end
```
Expand Down

0 comments on commit 1ff4932

Please sign in to comment.