跳转到内容
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附件属性
Unique identifierurl

使用 asset:// 或 file:// 协议的内容 URL

通道

注:在目标平台上测试附件以确保兼容性。

创建频道

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优先级级别(None, Min, Low, Default, High)
visibility隐私设置(Secret, Private, Public)
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

权限表

Identifier 描述

notification:allow-batch

启用批处理命令,无需预先配置范围。

notification:deny-batch

否认批处理命令,无需预先配置范围。

notification:allow-cancel

启用取消命令,无需预先配置范围。

notification:deny-cancel

否认取消命令,无需预先配置范围。

notification:allow-check-permissions

启用检查权限命令,无需预先配置范围。

notification:deny-check-permissions

否认检查权限命令,无需预先配置范围。

notification:allow-create-channel

启用创建频道命令,无需预先配置范围。

notification:deny-create-channel

否认创建频道命令,无需预先配置范围。

notification:allow-delete-channel

启用删除频道命令,无需预先配置范围。

notification:deny-delete-channel

否认删除频道命令,无需预先配置范围。

notification:allow-get-active

启用获取活动命令,无需预先配置范围。

notification:deny-get-active

否认获取活动命令,无需预先配置范围。

notification:allow-get-pending

启用获取挂起命令,无需预先配置范围。

notification:deny-get-pending

否认获取挂起命令,无需预先配置范围。

notification:allow-is-permission-granted

启用是权限已授予命令,无需预先配置范围。

notification:deny-is-permission-granted

否认是权限已授予命令,无需预先配置范围。

notification:allow-list-channels

启用列出频道命令,无需预先配置范围。

notification:deny-list-channels

否认列出频道命令,无需预先配置范围。

notification:allow-notify

启用通知命令,无需预先配置作用域。

notification:deny-notify

拒绝通知命令,无需预先配置作用域。

notification:allow-permission-state

启用权限状态命令,无需预先配置作用域。

notification:deny-permission-state

拒绝权限状态命令,无需预先配置作用域。

notification:allow-register-action-types

启用注册动作类型命令,无需预先配置作用域。

notification:deny-register-action-types

拒绝注册动作类型命令,无需预先配置作用域。

notification:allow-register-listener

启用注册监听器命令,无需预先配置作用域。

notification:deny-register-listener

拒绝注册监听器命令,无需预先配置作用域。

notification:allow-remove-active

启用移除活动命令,无需预先配置作用域。

notification:deny-remove-active

拒绝移除活动命令,无需预先配置作用域。

notification:allow-request-permission

启用请求权限命令,无需预先配置作用域。

notification:deny-request-permission

拒绝请求权限命令,无需预先配置作用域。

notification:allow-show

启用显示命令,无需预先配置作用域。

notification:deny-show

拒绝显示命令,无需预先配置作用域。


© 2025 Tauri 贡献者。CC-BY / MIT