Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ windows = { version = "0.61", features = [
"Foundation",
"Networking_PushNotifications",
"UI_Notifications",
"Win32_Foundation",
"Win32_System_Com",
"Win32_UI_Notifications",
] }
# The `#[implement]` macro from `windows::core::implement` emits `::windows_core::*`
# absolute paths, so `windows-core` must be a direct dependency, not just
# transitive through `windows`.
windows-core = "0.61"
# Used to parse user-supplied CLSID strings (accepts braced, unbraced, and
# hyphen-less forms) before converting to `windows::core::GUID` via raw bits.
uuid = { version = "1", default-features = false }
nt-time = "0.15"

[build-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions examples/notifications-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"tauri": "tauri"
"tauri": "tauri",
"tauri:windows:build": "tauri-windows-bundle build"
},
"license": "MIT",
"dependencies": {
Expand All @@ -25,6 +26,7 @@
"svelte": "^5.56.1",
"svelte-check": "^4.4.4",
"typescript": "~6.0.3",
"vite": "^8.0.13"
"vite": "^8.0.13",
"@choochmeque/tauri-windows-bundle": "^0.1.20"
}
}
473 changes: 473 additions & 0 deletions examples/notifications-demo/pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/notifications-demo/src-tauri/gen/windows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated files
# Keep bundle.config.json and templates in git
Comment on lines +1 to +2
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities">

<Identity
Name="{{PACKAGE_NAME}}"
Publisher="{{PUBLISHER}}"
Version="{{VERSION}}"
ProcessorArchitecture="{{ARCH}}" />

<Properties>
<DisplayName>{{DISPLAY_NAME}}</DisplayName>
<PublisherDisplayName>{{PUBLISHER_DISPLAY_NAME}}</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>

<Dependencies>
<TargetDeviceFamily Name="Windows.Desktop"
MinVersion="{{MIN_VERSION}}"
MaxVersionTested="10.0.22621.0" />
</Dependencies>

<Resources>
<Resource Language="en-us" />
</Resources>

<Applications>
<Application Id="App"
Executable="{{EXECUTABLE}}"
EntryPoint="Windows.FullTrustApplication">
<uap:VisualElements
DisplayName="{{DISPLAY_NAME}}"
Description="{{DESCRIPTION}}"
BackgroundColor="transparent"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
</uap:VisualElements>

{{EXTENSIONS}}
</Application>
</Applications>

<Capabilities>
{{CAPABILITIES}}
</Capabilities>
</Package>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"publisher": "CN=YourCompany",
"publisherDisplayName": "Your Company Name",
"capabilities": {
"general": [
"internetClient"
]
},
"extensions": {
"shareTarget": false,
"fileAssociations": [],
"protocolHandlers": [],
"toastActivation": {
"activationType": "foreground",
"clsid": "{757B33CB-099E-4AA9-8741-314D2B54B59D}"
}
},
"signing": {
"pfx": null,
"pfxPassword": null
},
"resourceIndex": {
"enabled": false,
"keepConfig": false
}
}
7 changes: 7 additions & 0 deletions examples/notifications-demo/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
"csp": null
}
},
"plugins": {
"notifications": {
"windows": {
"toastActivatorClsid": "{757B33CB-099E-4AA9-8741-314D2B54B59D}"
}
}
},
"bundle": {
"active": true,
"targets": "all",
Expand Down
37 changes: 33 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Send message notifications (brief auto-expiring OS window element) to your user. Can also be used with the Notification Web API.

use serde::Serialize;
use serde::{Deserialize, Serialize};
#[cfg(mobile)]
use tauri::plugin::PluginHandle;
#[cfg(desktop)]
Expand All @@ -10,6 +10,33 @@ use tauri::{
Manager, Runtime,
};

/// Top-level plugin config deserialized from the `plugins.notifications` block
/// in `tauri.conf.json`. Optional in its entirety — apps without a config block
/// get `PluginConfig::default()`.
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct PluginConfig {
#[cfg(target_os = "windows")]
pub windows: WindowsConfig,
}

/// Windows-only plugin config.
///
/// Currently carries the toast activator CLSID used by
/// `INotificationActivationCallback` registration; absent value means
/// COM-based activation is disabled and the plugin falls back to in-process
/// `Activated` events only.
#[cfg(target_os = "windows")]
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct WindowsConfig {
/// Toast activator CLSID. Must match the GUID declared in the MSIX
/// manifest's `<desktop:ToastNotificationActivation ToastActivatorCLSID>`
/// and `<com:Class Id>` entries. Accepts the `xxxxxxxx-xxxx-...` form
/// with or without surrounding braces.
pub toast_activator_clsid: Option<String>,
}

pub use models::*;
pub use tauri::plugin::PermissionState;

Expand Down Expand Up @@ -266,8 +293,8 @@ impl<R: Runtime, T: Manager<R>> crate::NotificationsExt<R> for T {

/// Initializes the plugin.
#[must_use]
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("notifications")
pub fn init<R: Runtime>() -> TauriPlugin<R, PluginConfig> {
Builder::<R, PluginConfig>::new("notifications")
.invoke_handler(tauri::generate_handler![
commands::notify,
commands::request_permission,
Expand Down Expand Up @@ -299,14 +326,16 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
.setup(|app, api| {
#[cfg(desktop)]
listeners::init();
#[cfg(all(target_os = "windows", not(feature = "notify-rust")))]
let windows_config = api.config().windows.clone();
#[cfg(mobile)]
let notification = mobile::init(app, api)?;
#[cfg(all(desktop, any(feature = "notify-rust", target_os = "linux")))]
let notification = desktop::init(app, api)?;
#[cfg(all(target_os = "macos", not(feature = "notify-rust")))]
let notification = macos::init(app, api)?;
#[cfg(all(target_os = "windows", not(feature = "notify-rust")))]
let notification = windows::init(app, api)?;
let notification = windows::init(app, api, windows_config)?;
app.manage(notification);
Ok(())
})
Expand Down
21 changes: 20 additions & 1 deletion src/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use std::collections::HashMap;
use std::sync::{OnceLock, RwLock};

#[cfg(all(target_os = "windows", not(feature = "notify-rust")))]
use tauri::Manager;
use tauri::{AppHandle, Runtime};

use crate::error::{ErrorResponse, PluginInvokeError};

type ChannelMap = HashMap<u32, tauri::ipc::Channel<serde_json::Value>>;
Expand Down Expand Up @@ -66,14 +70,21 @@ pub fn trigger(event: &str, payload: String) -> crate::Result<()> {
}

/// Register a channel to receive events for the given event name.
///
/// On Windows, subscribing to `notificationClicked` synchronously drains any
/// cold-start activation payload buffered by the COM activator before the JS
/// listener was up. That makes the `push-listener.tsx` contract ("subscribing
/// flushes the buffered tap") work without the app calling any extra command.
// Tauri commands receive serde-deserialized owned values.
#[allow(clippy::needless_pass_by_value)]
#[tauri::command]
pub fn register_listener(
pub fn register_listener<R: Runtime>(
app: AppHandle<R>,
event: String,
handler: tauri::ipc::Channel<serde_json::Value>,
) -> crate::Result<()> {
let listeners = LISTENERS.get_or_init(|| RwLock::new(HashMap::new()));
let should_drain_clicks = event == "notificationClicked";
{
let mut guard = listeners.write().map_err(|e| {
crate::Error::from(PluginInvokeError::InvokeRejected(ErrorResponse {
Expand All @@ -87,6 +98,14 @@ pub fn register_listener(
.or_default()
.insert(handler.id(), handler);
}
#[cfg(all(target_os = "windows", not(feature = "notify-rust")))]
if should_drain_clicks {
if let Some(notif) = app.try_state::<crate::Notifications<R>>() {
notif.drain_pending_clicks();
}
}
#[cfg(not(all(target_os = "windows", not(feature = "notify-rust"))))]
let _ = (app, should_drain_clicks);
Ok(())
}

Expand Down
Loading
Loading