Files
buzz/desktop/src-tauri/src/commands/notifications.rs
Kalvin C 7334ad1e16 fix(desktop): route macos notification clicks (#4799)
## Summary
- deliver macOS notifications through `UNUserNotificationCenter`
- route notification clicks to the referenced channel or thread through
the existing frontend activation path
- preserve click targets across cold startup and frontend remounts with
a small process-wide activation queue
- keep Linux notification activation behavior unchanged

## Architecture
A single `UNUserNotificationCenterDelegate` is installed during Tauri
setup. Each notification stores its navigation target in `userInfo`. On
click, Rust queues the target before emitting a wake-up event; the
frontend atomically drains the queue and dispatches the existing
notification action. The queue is the source of truth, which prevents
cold-start loss and duplicate delivery.

## Validation
Verified at `a81241611d617becf7640bee6fe56b5cdb4d0fab`:
- Biome format/check and lint
- TypeScript typecheck
- repository and desktop-Tauri `cargo fmt --check`
- repository and desktop-Tauri Clippy with `-D warnings`
- full pre-push desktop tests and Tauri workspace checks/tests
- desktop production build

Manual macOS validation passed: after explicitly ad-hoc signing the
local bundle with `xyz.block.buzz.app`, the operator confirmed real
Notification Center delivery and click navigation.

<details>
<summary>Local macOS test procedure</summary>

Tauri's generated ad-hoc signing identifier is not accepted by
`UNUserNotificationCenter`. Re-sign the local bundle with its bundle
identifier and keep other Buzz copies closed:

```bash
just desktop-release-build
APP="$HOME/.cache/cargo-target/aarch64-apple-darwin/release/bundle/macos/Buzz.app"
codesign --force --deep --sign - \
  --identifier xyz.block.buzz.app \
  --entitlements desktop/src-tauri/Entitlements.plist \
  "$APP"
codesign --verify --deep --strict --verbose=2 "$APP"
pkill -x buzz-desktop || true
open -n "$APP"
```

</details>

Buzz channel: `55e2bfca-1b38-48fb-9dc2-584d400501f3`

---------

Signed-off-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz>
Signed-off-by: am <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz>
Co-authored-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz>
2026-08-05 13:29:41 -07:00

109 lines
4.3 KiB
Rust

//! Native desktop-notification helpers.
//!
//! `tauri-plugin-notification` posts a notification by calling `notify_rust`'s
//! `show()` and then immediately dropping the returned `NotificationHandle`.
//! That handle owns the D-Bus connection used to post the notification, and on
//! GNOME 46+ (Ubuntu 24.04+, Fedora 41+) tearing that connection down dismisses
//! the notification the instant it appears — so notifications never show.
//! See tauri-apps/plugins-workspace#2566 and hoodie/notify-rust#218.
//!
//! We side-step the plugin on Linux by posting the notification from a
//! dedicated thread that holds the connection open (via `wait_for_action`)
//! until the notification is closed. The same wait surfaces the default click
//! action, which we forward to the frontend so it can focus the window and
//! route to the notification target.
pub(crate) const NATIVE_NOTIFICATION_ACTIVATED_EVENT: &str = "native-notification-activated";
/// Show a desktop notification natively.
///
/// Linux uses the connection-preserving D-Bus path described above. macOS uses
/// one application-lifetime `UNUserNotificationCenterDelegate`; it does not
/// allocate a listener or waiter for each notification.
#[tauri::command]
pub async fn show_native_notification(
app: tauri::AppHandle,
title: String,
body: Option<String>,
target: Option<serde_json::Value>,
) -> Result<(), String> {
#[cfg(target_os = "linux")]
{
linux::show(app, title, body, target);
Ok(())
}
#[cfg(target_os = "macos")]
{
let _ = app;
crate::macos_notifications::show(title, body, target).await
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
let _ = (&app, &title, &body, &target);
Err("show_native_notification is only supported on Linux and macOS".to_string())
}
}
#[cfg(target_os = "linux")]
mod linux {
use super::NATIVE_NOTIFICATION_ACTIVATED_EVENT;
use tauri::Emitter;
pub fn show(
app: tauri::AppHandle,
title: String,
body: Option<String>,
target: Option<serde_json::Value>,
) {
// notify_rust's `show()` blocks on D-Bus and the returned handle must
// outlive the notification, so this runs on its own thread rather than
// the async runtime.
std::thread::spawn(move || {
let mut builder = notify_rust::Notification::new();
builder.summary(&title);
if let Some(body) = body.as_deref() {
builder.body(body);
}
if let Some(name) = app.config().product_name.clone() {
builder.appname(&name);
}
// Tie the notification to the installed desktop entry so GNOME shows
// the app's name and icon and groups our notifications together.
builder.hint(notify_rust::Hint::DesktopEntry(
app.config().identifier.clone(),
));
builder.auto_icon();
// Match the silent posting used on other platforms; the app does its
// own unread cues and a per-message sound would be noisy.
builder.hint(notify_rust::Hint::SuppressSound(true));
// Declaring a default action makes the whole notification clickable.
builder.action("default", "Open");
let handle = match builder.show() {
Ok(handle) => handle,
Err(error) => {
eprintln!("buzz-desktop: failed to post native notification: {error}");
return;
}
};
// Block until the notification is actioned or closed. Holding the
// handle keeps its D-Bus connection alive, which is what stops
// GNOME 46+ from dismissing the notification immediately. The wait
// also returns when the notification expires or is dismissed, so
// the thread does not leak.
handle.wait_for_action(|action| {
if action != "default" {
return;
}
// The frontend focuses the window on activation (the same path
// every other platform uses), so we only forward the target.
let _ = app.emit(NATIVE_NOTIFICATION_ACTIVATED_EVENT, target);
});
});
}
}