Skip to content

Fix/retain notification reference#118

Open
gwleuverink wants to merge 5 commits into
mainfrom
fix/retain-notification-reference
Open

Fix/retain notification reference#118
gwleuverink wants to merge 5 commits into
mainfrom
fix/retain-notification-reference

Conversation

@gwleuverink
Copy link
Copy Markdown
Collaborator

Keep a reference to Notifications so their events fire

Main process notifications stop emitting click, close, action and reply events after a while. It is most noticeable on macOS when the app has been idle or in the background: the notification still shows, the app may even come to the foreground when you click it, but the handler we registered never runs, so nothing is sent back to PHP.

The cause is garbage collection. Electron only keeps a weak reference to a main process Notification, so if nothing on the JS side holds onto it, V8 can collect the wrapper before the user interacts with it. Once that happens the native notification has no JS object left to dispatch through and the event is silently dropped. This is documented behaviour, see electron/electron#16922.

In api/notification.ts the Notification was a local const that went out of scope as soon as the request handler returned, so it was a prime candidate for collection.

The fix

Hold onto each notification until it is dismissed. This mirrors how state.windows and state.processes are already kept alive in state.ts:

  • Add a notifications record to state.
  • Store the notification under its reference right before show().
  • Delete it again in the click and close handlers.

Tests

This route had no tests, so I added coverage for it in tests/notification.test.ts. It mounts the real router and exercises the POST endpoint:

  • the notification is retained in state after it is shown
  • click, action, reply and close are forwarded to Laravel with the right payloads
  • click and close release the retained reference
  • a local sound file is played by us and muted on the Electron notification, and a missing sound file logs an error instead

Notes

  • No API or behaviour change for consumers. Notifications that already worked keep working, the ones that were dropped now fire.
  • dist/ is left to CI to rebuild.

notifications could be garbage collected before the user interacts with it, after which the click / action / reply / close handlers silently never fire
@gwleuverink gwleuverink self-assigned this May 31, 2026
@gwleuverink gwleuverink added the bug Something isn't working label May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant