Skip to content

fix(macos): keep badges independent of notifications - #227

Merged
houko merged 3 commits into
mainfrom
fix/macos-badge-permission
Nov 16, 2026
Merged

houko merged 3 commits into
mainfrom
fix/macos-badge-permission

Conversation

@houko

@houko houko commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #190

A bundled macOS app that touches UNUserNotificationCenter gets its Dock badge shown only while the user allows the app's badges, so NSGo no longer touches the center of an app that never used notifications: SetBadge stays a plain NSApp.dockTile update and needs no permission.

  • macOS sets the notification delegate at launch only for an app that checked or asked for the permission or showed a notification in this or an earlier run, recorded in its defaults (NSGoNotifications). A press that launches the app, or one on a notification an earlier run showed, still reaches OnNotificationPress; an app using notifications for the first time sets the delegate when it does.
  • The delegate is made once (notificationDelegate), so setting it again from the permission request, a shown notification and launch no longer registers its class twice, which panicked.
  • CloseNotification and CloseNotifications leave the center of a macOS app that never used notifications untouched, so an app that clears them each time it comes to the front keeps its badge.
  • CHANGELOG.md gets an Unreleased line, now that 0.1.0 has shipped.
  • The notifications page (en, zh), the services architecture doc and the SetBadge, NotificationPermission and RequestNotificationPermission doc comments say that once a macOS app uses notifications (checks or asks for the permission, or shows a notification), its Dock badge shows only while the user allows its badges, which the permission request asks for.

Verification

  • go tool nsgo check -target all; go build ./... and builds for windows, linux, android/arm64 and js/wasm; go tool nsgo test on internal/platform/apple, internal/platform/macos, core and -run 'Layer|Skills|Doc' .. TestNotificationDelegateRegistersOnce panics on the first version of this change.
  • On macOS 27.2, with probe bundles signed with a Developer ID and run from ~/Applications, read through the Dock item's AXStatusLabel:
    • An app that only calls SetBadge(7) shows 7.
    • An app that asks for the permission shows no badge while the prompt waits, and 7 after Allow.
    • Pressing that app's notification after it quit launches it and the press reaches OnNotificationPress. With the first version of this change, which set the delegate only lazily, the same press was lost, and asking for the permission then showing a notification panicked with objc: failed to create class with name 'NsgoNotificationDelegate'.
  • iOS keeps setting the delegate at launch; its only change is that the delegate is made once. It could not be checked on a device.

Screenshots

None.

@github-actions github-actions Bot added documentation Improvements or additions to documentation macOS iOS size/S labels Nov 15, 2026
@houko houko changed the title Keep macOS badges independent of notifications fix(macos): keep badges independent of notifications Nov 15, 2026
@github-actions github-actions Bot added bug Something isn't working conflict Conflicts with the base branch; resolve it first labels Nov 15, 2026
@houko
houko force-pushed the fix/macos-badge-permission branch from b504389 to 964dffe Compare November 16, 2026 09:30
@github-actions github-actions Bot added core size/S size/M and removed size/S conflict Conflicts with the base branch; resolve it first labels Nov 16, 2026
@houko
houko force-pushed the fix/macos-badge-permission branch from 964dffe to 7f21f5f Compare November 16, 2026 09:32
@github-actions github-actions Bot removed the size/S label Nov 16, 2026
@houko
houko force-pushed the fix/macos-badge-permission branch from 7f21f5f to e894ba2 Compare November 16, 2026 09:32
@github-actions github-actions Bot added the conflict Conflicts with the base branch; resolve it first label Nov 16, 2026
Touching UNUserNotificationCenter makes macOS show a bundled app's Dock badge only while the user allows its badges, so NSGo no longer touches the center at launch; the delegate is set when the app asks for the permission or shows a notification.
…presses

Making WatchNotifications run from NotificationPermission and ShowNotification registered the delegate's class on every call, so a second call panicked: on iOS, which watches at launch, the first permission request; on macOS, the second notification. The delegate is now made once and setting it again changes nothing.

Watching only lazily also lost presses on macOS: a notification pressed after the app ended, or one an earlier run showed, is answered to the delegate before any code of this run asks for notifications. NSGo now records in the app's defaults that it asked for the permission or showed a notification, and macOS sets the delegate at launch for such an app, so only an app that never used notifications keeps the center untouched and its Dock badge free of any permission.

Closing notifications no longer touches the center of an app that never used them, as one clearing them each time it comes to the front, as the services doc suggests, would otherwise hide its Dock badge.

The docs and doc comments say that a macOS app's Dock badge needs the user to allow badges once it uses notifications.
…ons on macOS

NotificationPermission also touches UNUserNotificationCenter and records the use, so once a macOS app checks the permission its Dock badge shows only while the user allows its badges. The doc comments and pages said only asking or showing did.
@houko
houko force-pushed the fix/macos-badge-permission branch from e894ba2 to 9b22bd7 Compare November 16, 2026 10:10
@houko

houko commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator Author

Reviewed.

  • Code: the delegate class is registered once (sync.OnceValue), macOS sets the delegate at launch only when the NSGoNotifications defaults record exists, and CloseNotification returns early on macOS for an app without that record. iOS still sets the delegate at launch; its only change is the once-made delegate and the defaults record written on first use. The CHANGELOG line is under Unreleased, as the contributing rules require now that 0.1.0 has shipped.
  • Regression test: with notificationDelegate turned back into a plain function, TestNotificationDelegateRegistersOnce panics in RegisterClass; with the fix it passes.
  • Docs: NotificationPermission (checking without asking) also touches the center and records the use, so the badge gating starts there too. I added a commit saying so in the NotificationPermission, RequestNotificationPermission and SetBadge doc comments, the notifications page (en, zh) and the services architecture doc.
  • Rebased onto current main (one CHANGELOG conflict, both lines kept). After the rebase: go build ./... and builds for windows, linux, android/arm64 and js/wasm; go tool nsgo test ./core (570 passed) and -run 'Layer|Skills|Doc' . (29 passed) on Linux; on macOS, go vet and go tool nsgo test for internal/platform/apple (60 passed, 1 skipped) and internal/platform/macos (26 passed), and go tool nsgo check -target macos_arm64,ios. Before the last rebase, go tool nsgo check -target all also passed.
  • Real macOS 27.2 check with a fresh, ad hoc signed probe bundle. I read the Dock item's AXStatusLabel through System Events. Setting only SetBadge(7) showed 7 and wrote no record. Calling CloseNotifications and then SetBadge(7) also showed 7 and wrote no record. Calling NotificationPermission and then SetBadge(7) showed no badge (badges were not allowed) and wrote NSGoNotifications. After that, a badge-only launch also showed no badge, which matches the documented behaviour.
  • Not checked: iOS on a device. The press that launches the app was not re-checked here; the fixer checked it on macOS with a Developer ID build.

@github-actions github-actions Bot removed the conflict Conflicts with the base branch; resolve it first label Nov 16, 2026
@houko
houko marked this pull request as ready for review November 16, 2026 10:10
@houko
houko merged commit b8d3d4b into main Nov 16, 2026
19 checks passed
@houko
houko deleted the fix/macos-badge-permission branch November 16, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working core documentation Improvements or additions to documentation iOS macOS size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: SetBadge is invisible in a bundled app without notification permission

1 participant