mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-26 03:46:25 -08:00
feat(tauri): show desktop notifications via the native plugin on Linux and macOS
This commit is contained in:
1 parent
825af1aa8e
commit
c8ebb82744
4 files changed
+48
-5
No files matched your search
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -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)]
|
||||
|
||||
@@ -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<void> {
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user