gitbay/Push/AppBadge.swift
19 lines · 905 bytes
1import UserNotifications
2
3/// Sets the app icon badge to an unread count.
4///
5/// A push carries `aps.badge` and iOS applies it on arrival, so the badge
6/// is right the moment a notification lands. It is not right afterwards:
7/// nothing tells iOS the count dropped when the inbox is read in the app,
8/// and the number sits there until the next push. These calls close that.
9///
10/// The value is always the active account's unread count. The badge is
11/// per-device and unread is per-account, so with two accounts signed in
12/// the last writer wins — a push for one account, then a dashboard load
13/// on the other, disagree. Exact for a single account.
14///
15/// Failures are ignored. A badge that did not take is not worth telling
16/// anyone about, and the next writer corrects it.
17func setAppBadge(_ count: Int) async {
18 try? await UNUserNotificationCenter.current().setBadgeCount(max(0, count))
19}