|
| 1 | +// Copyright 2017-2022 allenbenz <[email protected]> |
| 2 | +// Copyright 2022-2025 Tauri Programme within The Commons Conservancy |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +use std::{thread::sleep, time::Duration}; |
| 7 | + |
| 8 | +use tauri_winrt_notification::Toast; |
| 9 | +use windows_registry::CURRENT_USER; |
| 10 | + |
| 11 | +// App's AUMID (Application User Model Id), should be unique and no longer than 129 characters. |
| 12 | +// https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast-other-apps#aumid-restrictions |
| 13 | +const APP_ID: &str = "tauri.UnpackagedAppWinRtNotificationExample"; |
| 14 | + |
| 15 | +// App's display name |
| 16 | +const APP_NAME: &str = "Unpackaged App"; |
| 17 | + |
| 18 | +fn main() -> windows_registry::Result<()> { |
| 19 | + init_registry()?; |
| 20 | + |
| 21 | + Toast::new(APP_ID) |
| 22 | + .title("Notification from an unpackaged app") |
| 23 | + .scenario(tauri_winrt_notification::Scenario::Reminder) |
| 24 | + .add_button("OK", "ok") |
| 25 | + .show() |
| 26 | + .expect("unable to send notification"); |
| 27 | + |
| 28 | + // The notification won't appear if we clean up registry too early |
| 29 | + sleep(Duration::from_secs(3)); |
| 30 | + clean_up_registry() |
| 31 | +} |
| 32 | + |
| 33 | +// Create registry key for this example |
| 34 | +fn init_registry() -> windows_registry::Result<()> { |
| 35 | + let icon_path = std::env::current_dir()?.join(r"resources\tauri.png"); |
| 36 | + |
| 37 | + let key = CURRENT_USER.create(format!(r"SOFTWARE\Classes\AppUserModelId\{APP_ID}"))?; |
| 38 | + key.set_string("DisplayName", APP_NAME)?; |
| 39 | + key.set_string("IconBackgroundColor", "0")?; |
| 40 | + key.set_hstring("IconUri", &icon_path.as_path().into()) |
| 41 | +} |
| 42 | + |
| 43 | +// Remove this example from the registry |
| 44 | +fn clean_up_registry() -> windows_registry::Result<()> { |
| 45 | + CURRENT_USER.remove_tree(format!(r"SOFTWARE\Classes\AppUserModelId\{APP_ID}")) |
| 46 | +} |
0 commit comments