Commit a866e469a3
a866e469a3568bf7a2829a8dd2a56317ced4736e
parent: effed25309
Verified · cmc
cmc <hello@cleberg.net> · 2026-10-10 19:42 UTC
iOS: ask for badges when they're off
Notifications granted before the app asked for badges leave Badges out of its notification settings. The app asks again while badges are off, which adds them; Settings has a button that asks and, when badges stay off, opens the app's notification settings.
Layout: unified · split
Sources/OrgstarMobile/MobileModel.swift
+12 −1
| @@ -272,7 +272,7 @@ final class MobileModel { |
| 272 | /// that only changes the badge. |
272 | /// that only changes the badge. |
| 273 | private func updateBadge(_ counts: [Int]) async { |
273 | private func updateBadge(_ counts: [Int]) async { |
| 274 | let center = UNUserNotificationCenter.current() |
274 | let center = UNUserNotificationCenter.current() |
| 275 | guard await SystemReminderCenter().authorize(), await center.notificationSettings().badgeSetting == .enabled else { return } |
275 | guard await badgesEnabled(asking: true) else { return } |
| 276 | try? await center.setBadgeCount(counts.first ?? 0) |
276 | try? await center.setBadgeCount(counts.first ?? 0) |
| 277 | center.removePendingNotificationRequests(withIdentifiers: (1..<AgendaSnapshot.dayCount).map(Self.badgeID)) |
277 | center.removePendingNotificationRequests(withIdentifiers: (1..<AgendaSnapshot.dayCount).map(Self.badgeID)) |
| 278 | let today = Calendar.current.startOfDay(for: Date()) |
278 | let today = Calendar.current.startOfDay(for: Date()) |
| @@ -287,5 +287,16 @@ final class MobileModel { |
| 287 | } |
287 | } |
| 288 | |
288 | |
| 289 | static func badgeID(_ offset: Int) -> String { "badge.\(offset)" } |
289 | static func badgeID(_ offset: Int) -> String { "badge.\(offset)" } |
| |
290 | |
| |
291 | /// Whether iOS shows the icon's badge. Asking adds badges to the app's notification |
| |
292 | /// settings, which an earlier grant without them leaves out; iOS prompts only the first time. |
| |
293 | func badgesEnabled(asking: Bool) async -> Bool { |
| |
294 | let center = UNUserNotificationCenter.current() |
| |
295 | let settings = await center.notificationSettings() |
| |
296 | if settings.badgeSetting == .enabled { return true } |
| |
297 | guard asking, settings.authorizationStatus != .denied else { return false } |
| |
298 | _ = try? await center.requestAuthorization(options: [.alert, .sound, .badge]) |
| |
299 | return await center.notificationSettings().badgeSetting == .enabled |
| |
300 | } |
| 290 | } |
301 | } |
| 291 | #endif |
302 | #endif |
Sources/OrgstarMobile/SettingsScreen.swift
+25
| @@ -11,6 +11,9 @@ struct SettingsScreen: View { |
| 11 | let model: MobileModel |
11 | let model: MobileModel |
| 12 | @State private var choosing = false |
12 | @State private var choosing = false |
| 13 | @State private var calendars: [CalendarChoice] = [] |
13 | @State private var calendars: [CalendarChoice] = [] |
| |
14 | @State private var badges: Bool? |
| |
15 | @Environment(\.openURL) private var openURL |
| |
16 | @Environment(\.scenePhase) private var scenePhase |
| 14 | |
17 | |
| 15 | private struct CalendarChoice { |
18 | private struct CalendarChoice { |
| 16 | let id: String |
19 | let id: String |
| @@ -59,6 +62,26 @@ struct SettingsScreen: View { |
| 59 | } footer: { |
62 | } footer: { |
| 60 | Text("org-enforce-todo-dependencies and org-enforce-todo-checkbox-dependencies.") |
63 | Text("org-enforce-todo-dependencies and org-enforce-todo-checkbox-dependencies.") |
| 61 | } |
64 | } |
| |
65 | Section { |
| |
66 | if badges == true { |
| |
67 | LabeledContent("Overdue Count on Icon", value: "On") |
| |
68 | } else if badges == false { |
| |
69 | Button("Show Overdue Count on Icon…") { |
| |
70 | Task { |
| |
71 | if await model.badgesEnabled(asking: true) { |
| |
72 | badges = true |
| |
73 | await model.updateWidgets() |
| |
74 | } else if let url = URL(string: UIApplication.openNotificationSettingsURLString) { |
| |
75 | openURL(url) |
| |
76 | } |
| |
77 | } |
| |
78 | } |
| |
79 | } |
| |
80 | } header: { |
| |
81 | Text("Badge") |
| |
82 | } footer: { |
| |
83 | if badges == false { Text("Turn on Badges in orgstar's notification settings.") } |
| |
84 | } |
| 62 | calendarSection |
85 | calendarSection |
| 63 | Section("In use") { |
86 | Section("In use") { |
| 64 | let _ = model.settingsVersion |
87 | let _ = model.settingsVersion |
| @@ -71,6 +94,8 @@ struct SettingsScreen: View { |
| 71 | } |
94 | } |
| 72 | } |
95 | } |
| 73 | .navigationTitle("Settings") |
96 | .navigationTitle("Settings") |
| |
97 | // Read again on return from the Settings app. |
| |
98 | .task(id: scenePhase) { badges = await model.badgesEnabled(asking: false) } |
| 74 | .fileImporter(isPresented: $choosing, allowedContentTypes: [.folder]) { result in |
99 | .fileImporter(isPresented: $choosing, allowedContentTypes: [.folder]) { result in |
| 75 | if case .success(let url) = result { model.setConfigFolder(url) } |
100 | if case .success(let url) = result { model.setConfigFolder(url) } |
| 76 | } |
101 | } |
docs/manual/guide/14-ios.org
+1 −1
| @@ -262,7 +262,7 @@ With =reminders= on, orgstar schedules a notification before each timed agenda e |
| 262 | |
262 | |
| 263 | iOS lets an app schedule at most 64 notifications, and orgstar schedules more only while it runs. It updates them when files or settings change, when you return to the app, and every hour while it is open. The agenda's status line says how far ahead reminders are set ("Reminders are set through …"), or that notifications are off for orgstar in the system Settings app. |
263 | iOS lets an app schedule at most 64 notifications, and orgstar schedules more only while it runs. It updates them when files or settings change, when you return to the app, and every hour while it is open. The agenda's status line says how far ahead reminders are set ("Reminders are set through …"), or that notifications are off for orgstar in the system Settings app. |
| 264 | |
264 | |
| 265 | The app icon's badge counts the entries in the agenda's Overdue section that aren't done: past their scheduled day or their deadline, habits left out. An entry past both counts once. orgstar sets each of the next six midnights' counts ahead, so the badge stays right while the app is closed. Turn it off in Settings ▸ Notifications ▸ orgstar ▸ Badges. |
265 | The app icon's badge counts the entries in the agenda's Overdue section that aren't done: past their scheduled day or their deadline, habits left out. An entry past both counts once. orgstar sets each of the next six midnights' counts ahead, so the badge stays right while the app is closed. If notifications were allowed before orgstar asked for badges, tap Show Overdue Count on Icon under Badge in orgstar's Settings; it adds Badges to orgstar's notification settings and opens them when they're still off. Turn the badge off there, under Badges. |
| 266 | |
266 | |
| 267 | * Export |
267 | * Export |
| 268 | |
268 | |