From c8ebb827441a3870ccd3cd64c82d843a5c3e415e Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sat, 18 Jul 2026 20:18:32 +0200 Subject: [PATCH] feat(tauri): show desktop notifications via the native plugin on Linux and macOS --- src-tauri/Cargo.toml | 1 + src-tauri/capabilities/desktop.json | 18 ++++++++--- src-tauri/src/lib.rs | 2 +- .../pages/client/BackgroundNotifications.tsx | 32 +++++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 75eac3e36..843f758b5 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -56,6 +56,7 @@ windows = { version = "0.61", features = [ [target.'cfg(any(target_os = "macos", windows, target_os = "linux"))'.dependencies] tauri-plugin-single-instance = { version = "2.4.3", features = ["deep-link"] } +tauri-plugin-notifications = { git = "https://github.com/Choochmeque/tauri-plugin-notifications.git", rev = "2ff1373d218ff02a93f71f589c56e4e8b588ea7d" } [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies] tauri-plugin-window-state = "2.4.1" diff --git a/src-tauri/capabilities/desktop.json b/src-tauri/capabilities/desktop.json index 0e665f7fd..6c37a6ca2 100644 --- a/src-tauri/capabilities/desktop.json +++ b/src-tauri/capabilities/desktop.json @@ -1,7 +1,17 @@ { "$schema": "../gen/schemas/desktop-schema.json", "identifier": "desktop-capability", - "platforms": ["macOS", "windows", "linux"], - "windows": ["main"], - "permissions": ["window-state:default", "store:default"] -} + "platforms": [ + "macOS", + "windows", + "linux" + ], + "windows": [ + "main" + ], + "permissions": [ + "window-state:default", + "store:default", + "notifications:default" + ] +} \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 660d97887..9e71fd7bb 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -225,7 +225,7 @@ pub fn run() { let _ = show_or_create_main_window(app); })); - #[cfg(mobile)] + #[cfg(any(mobile, desktop))] let builder = builder.plugin(tauri_plugin_notifications::init()); #[cfg(mobile)] diff --git a/src/app/pages/client/BackgroundNotifications.tsx b/src/app/pages/client/BackgroundNotifications.tsx index 62d261742..44cc1d23f 100644 --- a/src/app/pages/client/BackgroundNotifications.tsx +++ b/src/app/pages/client/BackgroundNotifications.tsx @@ -45,6 +45,8 @@ import * as Sentry from '@sentry/react'; import { startClient, stopClient } from '$client/initMatrix'; import { SessionOidcTokenRefresher } from '$client/oidcTokenRefresher'; import { mobileOrTablet } from '$utils/user-agent'; +import { isTauri } from '@tauri-apps/api/core'; +import { type as osType } from '@tauri-apps/plugin-os'; const log = createLogger('BackgroundNotifications'); const debugLog = createDebugLogger('BackgroundNotifications'); @@ -52,6 +54,18 @@ const debugLog = createDebugLogger('BackgroundNotifications'); const BACKGROUND_SYNC_POLL_TIMEOUT_MS = 60_000; const BACKGROUND_STAGGER_DELAY_MS = 5_000; +// Desktop webviews can't show web notifications (WKWebView lacks the API; the +// Linux CEF runtime never grants it), so desktop routes through the native plugin. +const DESKTOP_TAURI_OS = new Set(['linux', 'macos', 'windows']); +const isDesktopTauri = (): boolean => isTauri() && DESKTOP_TAURI_OS.has(osType()); + +let desktopNotificationSeq = 1; +const nextDesktopNotificationId = (): number => { + const id = desktopNotificationSeq; + desktopNotificationSeq = desktopNotificationSeq >= 2_000_000_000 ? 1 : desktopNotificationSeq + 1; + return id; +}; + const isClientReadyForNotifications = (state: SyncState | string | null): boolean => state === SyncState.Prepared || state === SyncState.Syncing || state === SyncState.Catchup; @@ -201,6 +215,24 @@ export function BackgroundNotifications() { const activeIds = new Set(inactiveSessions.map((s) => s.userId)); async function sendNotification(opts: NotifyOptions): Promise { + if (isDesktopTauri()) { + try { + const { getTauriNotificationsApi } = await import( + '$features/settings/notifications/TauriNotificationsApiClient' + ); + const api = await getTauriNotificationsApi(); + await api.sendNotification({ + id: nextDesktopNotificationId(), + title: opts.title, + body: opts.body, + silent: opts.silent ?? false, + }); + return; + } catch (err) { + log.error('failed to show native desktop notification', err); + // Fall through to the web Notification path as a best-effort fallback. + } + } // Prefer ServiceWorkerRegistration.showNotification so that taps are handled // by the SW notificationclick event. This routes through HandleNotificationClick // (postMessage path) which does the account switch + deep link reliably on all