Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the iOS documentation on how to manage the badge #409

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 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,62 @@ 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 databse.
excid3 marked this conversation as resolved.
Show resolved Hide resolved

```ruby
class NativeBadgeNotifier < Noticed::Ephemeral
deliver_by :ios do |config|
config.format = ->(apn) {
apn.alert = ios_alert
apn.custom_payload = ios_custom_payload
excid3 marked this conversation as resolved.
Show resolved Hide resolved
apn.badge = recipient.notifications.unread.count
}
end

notification_methods do
def ios_custom_payload
{}
end

# 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
def ios_alert
nil
end
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