通知
使用通知外掛程式向使用者發送原生通知。
支援平台
此外掛程式需要至少 1.77.2 的 Rust 版本
平台 | 層級 | 注意事項 |
---|---|---|
windows | 僅適用於已安裝的應用程式。在開發中顯示 powershell 名稱和圖示。 | |
linux | ||
macos | ||
android | ||
ios |
設定
安裝通知外掛程式以開始使用。
使用專案的套件管理器新增依賴項
npm run tauri add notification
yarn run tauri add notification
pnpm tauri add notification
deno task tauri add notification
bun tauri add notification
cargo tauri add notification
-
在
src-tauri
資料夾中執行以下命令,將外掛程式新增至Cargo.toml
中的專案依賴項cargo add tauri-plugin-notification -
修改
lib.rs
以初始化外掛程式src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_notification::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
如果您想在 JavaScript 中使用通知,也請安裝 npm 套件
npm install @tauri-apps/plugin-notificationyarn add @tauri-apps/plugin-notificationpnpm add @tauri-apps/plugin-notificationbun add npm:@tauri-apps/plugin-notificationbun add @tauri-apps/plugin-notification
用法
以下是如何使用通知外掛程式的一些範例
通知外掛程式在 JavaScript 和 Rust 中均可用。
發送通知
請按照以下步驟發送通知
-
檢查是否已授予權限
-
如果未授予權限,則請求權限
-
發送通知
import { isPermissionGranted, requestPermission, sendNotification,} from '@tauri-apps/plugin-notification';// when using `"withGlobalTauri": true`, you may use// const { isPermissionGranted, requestPermission, sendNotification, } = window.__TAURI__.notification;
// Do you have permission to send a notification?let permissionGranted = await isPermissionGranted();
// If not we need to request itif (!permissionGranted) { const permission = await requestPermission(); permissionGranted = permission === 'granted';}
// Once permission has been granted we can send the notificationif (permissionGranted) { sendNotification({ title: 'Tauri', body: 'Tauri is awesome!' });}
tauri::Builder::default() .plugin(tauri_plugin_notification::init()) .setup(|app| { use tauri_plugin_notification::NotificationExt; app.notification() .builder() .title("Tauri") .body("Tauri is awesome") .show() .unwrap();
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");
動作
動作將互動式按鈕和輸入新增至通知。使用它們為您的使用者建立響應式體驗。
註冊動作類型
註冊動作類型以定義互動式元素
import { registerActionTypes } from '@tauri-apps/plugin-notification';
await registerActionTypes([ { id: 'messages', actions: [ { id: 'reply', title: 'Reply', input: true, inputButtonTitle: 'Send', inputPlaceholder: 'Type your reply...', }, { id: 'mark-read', title: 'Mark as Read', foreground: false, }, ], },]);
動作屬性
屬性 | 描述 |
---|---|
id | 動作的唯一識別碼 |
title | 動作按鈕的顯示文字 |
requiresAuthentication | 需要裝置驗證 |
foreground | 觸發時將應用程式帶到前景 |
destructive | 在 iOS 上以紅色顯示動作 |
input | 啟用文字輸入 |
inputButtonTitle | 輸入提交按鈕的文字 |
inputPlaceholder | 輸入欄位的預留位置文字 |
監聽動作
監聽使用者與通知動作的互動
import { onAction } from '@tauri-apps/plugin-notification';
await onAction((notification) => { console.log('Action performed:', notification);});
附件
附件將媒體內容新增至通知。支援程度因平台而異。
import { sendNotification } from '@tauri-apps/plugin-notification';
sendNotification({ title: 'New Image', body: 'Check out this picture', attachments: [ { id: 'image-1', url: 'asset:///notification-image.jpg', }, ],});
附件屬性
屬性 | 描述 |
---|---|
id | 唯一識別碼 |
url | 使用 asset:// 或 file:// 協定的內容 URL |
注意:在目標平台上測試附件,以確保相容性。
頻道
頻道將通知組織成具有不同行為的類別。雖然主要在 Android 上使用,但它們在各平台之間提供一致的 API。
建立頻道
import { createChannel, Importance, Visibility,} from '@tauri-apps/plugin-notification';
await createChannel({ id: 'messages', name: 'Messages', description: 'Notifications for new messages', importance: Importance.High, visibility: Visibility.Private, lights: true, lightColor: '#ff0000', vibration: true, sound: 'notification_sound',});
頻道屬性
屬性 | 描述 |
---|---|
id | 唯一識別碼 |
name | 顯示名稱 |
description | 用途描述 |
importance | 優先級別(無、最小、低、預設、高) |
visibility | 隱私設定(秘密、私密、公開) |
lights | 啟用通知 LED(Android) |
lightColor | LED 顏色(Android) |
vibration | 啟用震動 |
sound | 自訂聲音檔名 |
管理頻道
列出現有頻道
import { channels } from '@tauri-apps/plugin-notification';
const existingChannels = await channels();
移除頻道
import { removeChannel } from '@tauri-apps/plugin-notification';
await removeChannel('messages');
使用頻道
使用頻道發送通知
import { sendNotification } from '@tauri-apps/plugin-notification';
sendNotification({ title: 'New Message', body: 'You have a new message', channelId: 'messages',});
注意:在發送引用頻道的通知之前,請先建立頻道。無效的頻道 ID 會阻止通知顯示。
安全考量
除了正常的使用者輸入清理程序外,目前沒有已知的安全考量。
預設權限
此權限集配置預設公開的通知功能。
已授予的權限
它允許所有通知相關功能。
allow-is-permission-granted
allow-request-permission
allow-notify
allow-register-action-types
allow-register-listener
allow-cancel
allow-get-pending
allow-remove-active
allow-get-active
allow-check-permissions
allow-show
allow-batch
allow-list-channels
allow-delete-channel
allow-create-channel
allow-permission-state
權限表
識別碼 | 描述 |
---|---|
|
啟用 batch 命令,無需任何預先設定的範圍。 |
|
拒絕 batch 命令,無需任何預先設定的範圍。 |
|
啟用 cancel 命令,無需任何預先設定的範圍。 |
|
拒絕 cancel 命令,無需任何預先設定的範圍。 |
|
啟用 check_permissions 命令,無需任何預先設定的範圍。 |
|
拒絕 check_permissions 命令,無需任何預先設定的範圍。 |
|
啟用 create_channel 命令,無需任何預先設定的範圍。 |
|
拒絕 create_channel 命令,無需任何預先設定的範圍。 |
|
啟用 delete_channel 命令,無需任何預先設定的範圍。 |
|
拒絕 delete_channel 命令,無需任何預先設定的範圍。 |
|
啟用 get_active 命令,無需任何預先設定的範圍。 |
|
拒絕 get_active 命令,無需任何預先設定的範圍。 |
|
啟用 get_pending 命令,無需任何預先設定的範圍。 |
|
拒絕 get_pending 命令,無需任何預先設定的範圍。 |
|
啟用 is_permission_granted 命令,無需任何預先設定的範圍。 |
|
拒絕 is_permission_granted 命令,無需任何預先設定的範圍。 |
|
啟用 list_channels 命令,無需任何預先設定的範圍。 |
|
拒絕 list_channels 命令,無需任何預先設定的範圍。 |
|
啟用 notify 命令,無需任何預先設定的範圍。 |
|
拒絕 notify 命令,無需任何預先設定的範圍。 |
|
啟用 permission_state 命令,無需任何預先設定的範圍。 |
|
拒絕 permission_state 命令,無需任何預先設定的範圍。 |
|
啟用 register_action_types 命令,無需任何預先設定的範圍。 |
|
拒絕 register_action_types 命令,無需任何預先設定的範圍。 |
|
啟用 register_listener 命令,無需任何預先設定的範圍。 |
|
拒絕 register_listener 命令,無需任何預先設定的範圍。 |
|
啟用 remove_active 命令,無需任何預先設定的範圍。 |
|
拒絕 remove_active 命令,無需任何預先設定的範圍。 |
|
啟用 request_permission 命令,無需任何預先設定的範圍。 |
|
拒絕 request_permission 命令,無需任何預先設定的範圍。 |
|
啟用 show 命令,無需任何預先設定的範圍。 |
|
拒絕 show 命令,無需任何預先設定的範圍。 |
© 2025 Tauri 貢獻者。CC-BY / MIT