通知
使用通知插件向用户发送原生通知。
支持的平台
此插件需要 Rust 版本至少为 1.77.2
平台 | 级别 | 备注 |
---|---|---|
windows | 仅适用于已安装的应用程序。在开发中显示 powershell 名称和图标。 | |
linux | ||
macos | ||
android | ||
ios |
设置
安装 notifications 插件以开始使用。
使用项目的包管理器添加依赖项
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