跳到內容
Tauri

通知

使用通知外掛程式向使用者發送原生通知。

支援平台

此外掛程式需要至少 1.77.2 的 Rust 版本

平台 層級 注意事項
windows

僅適用於已安裝的應用程式。在開發中顯示 powershell 名稱和圖示。

linux
macos
android
ios

設定

安裝通知外掛程式以開始使用。

使用專案的套件管理器新增依賴項

npm run tauri add notification

用法

以下是如何使用通知外掛程式的一些範例

通知外掛程式在 JavaScript 和 Rust 中均可用。

發送通知

請按照以下步驟發送通知

  1. 檢查是否已授予權限

  2. 如果未授予權限,則請求權限

  3. 發送通知

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 it
if (!permissionGranted) {
const permission = await requestPermission();
permissionGranted = permission === 'granted';
}
// Once permission has been granted we can send the notification
if (permissionGranted) {
sendNotification({ title: 'Tauri', body: 'Tauri is awesome!' });
}

動作

動作將互動式按鈕和輸入新增至通知。使用它們為您的使用者建立響應式體驗。

註冊動作類型

註冊動作類型以定義互動式元素

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)
lightColorLED 顏色(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

權限表

識別碼 描述

notification:allow-batch

啟用 batch 命令,無需任何預先設定的範圍。

notification:deny-batch

拒絕 batch 命令,無需任何預先設定的範圍。

notification:allow-cancel

啟用 cancel 命令,無需任何預先設定的範圍。

notification:deny-cancel

拒絕 cancel 命令,無需任何預先設定的範圍。

notification:allow-check-permissions

啟用 check_permissions 命令,無需任何預先設定的範圍。

notification:deny-check-permissions

拒絕 check_permissions 命令,無需任何預先設定的範圍。

notification:allow-create-channel

啟用 create_channel 命令,無需任何預先設定的範圍。

notification:deny-create-channel

拒絕 create_channel 命令,無需任何預先設定的範圍。

notification:allow-delete-channel

啟用 delete_channel 命令,無需任何預先設定的範圍。

notification:deny-delete-channel

拒絕 delete_channel 命令,無需任何預先設定的範圍。

notification:allow-get-active

啟用 get_active 命令,無需任何預先設定的範圍。

notification:deny-get-active

拒絕 get_active 命令,無需任何預先設定的範圍。

notification:allow-get-pending

啟用 get_pending 命令,無需任何預先設定的範圍。

notification:deny-get-pending

拒絕 get_pending 命令,無需任何預先設定的範圍。

notification:allow-is-permission-granted

啟用 is_permission_granted 命令,無需任何預先設定的範圍。

notification:deny-is-permission-granted

拒絕 is_permission_granted 命令,無需任何預先設定的範圍。

notification:allow-list-channels

啟用 list_channels 命令,無需任何預先設定的範圍。

notification:deny-list-channels

拒絕 list_channels 命令,無需任何預先設定的範圍。

notification:allow-notify

啟用 notify 命令,無需任何預先設定的範圍。

notification:deny-notify

拒絕 notify 命令,無需任何預先設定的範圍。

notification:allow-permission-state

啟用 permission_state 命令,無需任何預先設定的範圍。

notification:deny-permission-state

拒絕 permission_state 命令,無需任何預先設定的範圍。

notification:allow-register-action-types

啟用 register_action_types 命令,無需任何預先設定的範圍。

notification:deny-register-action-types

拒絕 register_action_types 命令,無需任何預先設定的範圍。

notification:allow-register-listener

啟用 register_listener 命令,無需任何預先設定的範圍。

notification:deny-register-listener

拒絕 register_listener 命令,無需任何預先設定的範圍。

notification:allow-remove-active

啟用 remove_active 命令,無需任何預先設定的範圍。

notification:deny-remove-active

拒絕 remove_active 命令,無需任何預先設定的範圍。

notification:allow-request-permission

啟用 request_permission 命令,無需任何預先設定的範圍。

notification:deny-request-permission

拒絕 request_permission 命令,無需任何預先設定的範圍。

notification:allow-show

啟用 show 命令,無需任何預先設定的範圍。

notification:deny-show

拒絕 show 命令,無需任何預先設定的範圍。


© 2025 Tauri 貢獻者。CC-BY / MIT