Skip to content

Commit

Permalink
Update the iOS documentation on how to manage the badge (#409)
Browse files Browse the repository at this point in the history
* Update the iOS documentation on how to manage the badge

* Update docs/delivery_methods/ios.md

* Simplify example

---------

Co-authored-by: Chris Oliver <[email protected]>
  • Loading branch information
mark-kraemer and excid3 authored Feb 17, 2024
1 parent a7c2de0 commit 795d888
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions docs/delivery_methods/ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_
## Usage

```ruby
class CommentNotification
class CommentNotifier < ApplicationNotifier
deliver_by :ios do |config|
config.device_tokens = -> { recipient.notification_tokens.where(platform: :iOS).pluck(:token) }
config.format = ->(apn) {
Expand Down Expand Up @@ -83,13 +83,54 @@ end
Apple Push Notifications may fail delivery if the user has removed the app from their device. Noticed allows you

```ruby
class CommentNotification
class CommentNotifier < ApplicationNotifier
deliver_by :ios do |config|
config.invalid_token = ->(token) { NotificationToken.where(token: token).destroy_all }
end
end
```

## Updating the iOS app badge

If you're managing the iOS app badge, you can pass it along in the format

```ruby
class CommentNotifier < ApplicationNotifier
deliver_by :ios do |config|
config.format = ->(apn) {
apn.alert = "Hello world"
apn.custom_payload = {url: root_url(host: "example.org")}
apn.badge = recipient.notifications.unread.count
}
end
end
```

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.

```ruby
class NativeBadgeNotifier < Noticed::Ephemeral
deliver_by :ios do |config|
config.format = ->(apn) {
# Setting the alert text to nil will deliver the notification in
# the background. This is used to update the app badge on the iOS home screen
apn.alert = nil
apn.custom_payload = {}
apn.badge = recipient.notifications.unread.count
}
end
end
```

Then you can simply deliver this notifier to update the badge when you mark the notification as read

```ruby
notification.mark_as_read!
NativeBadgeNotifier.with(record: notification).deliver(notification.recipient)
```

## Delivering to Sandboxes and real devices

If you wish to send notifications to both sandboxed and real devices from the same application, you can configure two iOS delivery methods
Expand Down

0 comments on commit 795d888

Please sign in to comment.