跳到内容
Tauri

通知

使用通知插件向用户发送原生通知。

支持的平台

此插件需要 Rust 版本至少为 1.77.2

平台 级别 备注
windows

仅适用于已安装的应用程序。在开发中显示 powershell 名称和图标。

linux
macos
android
ios

设置

安装 notifications 插件以开始使用。

使用项目的包管理器添加依赖项

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