通知
使用通知插件向你的用户发送原生通知。
¥Send native notifications to your user using the notification plugin.
支持的平台
¥Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | Only works for installed apps. Shows powershell name & icon in development. | |
linux | ||
macos | ||
android | ||
ios |
设置
¥Setup
安装通知插件即可开始使用。
¥Install the notifications plugin to get started.
使用项目的包管理器添加依赖:
¥Use your project’s package manager to add the dependency:
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
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-notification -
Modify
lib.rs
to initialize the plugin: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");} -
If you’d like to use notifications in JavaScript then install the npm package as well:
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
使用
¥Usage
以下是一些如何使用通知插件的示例:
¥Here are a few examples of how to use the notification plugin:
通知插件在 JavaScript 和 Rust 中都可用。
¥The notification plugin is available in both JavaScript and Rust.
发送通知
¥Send Notification
按照以下步骤发送通知:
¥Follow these steps to send a notification:
-
检查是否授予权限
¥Check if permission is granted
-
如果未授予,则请求权限
¥Request permission if not granted
-
发送通知
¥Send the notification
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");
操作
¥Actions
操作为通知添加交互式按钮和输入。使用它们为你的用户创建响应式体验。
¥Actions add interactive buttons and inputs to notifications. Use them to create a responsive experience for your users.
注册操作类型
¥Register Action Types
注册动作类型以定义交互元素:
¥Register action types to define interactive elements:
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, }, ], },]);
操作属性
¥Action Properties
属性 | 描述 |
---|---|
id | 操作的唯一标识符 |
title | 显示操作按钮的文本 |
requiresAuthentication | 需要设备身份验证 |
foreground | 触发时将应用带到前台 |
destructive | 在 iOS 上以红色显示操作 |
input | 启用文本输入 |
inputButtonTitle | 输入提交按钮的文本 |
inputPlaceholder | 输入字段的占位符文本 |
监听操作
¥Listen for Actions
监听用户与通知操作的交互:
¥Listen to user interactions with notification actions:
import { onAction } from '@tauri-apps/plugin-notification';
await onAction((notification) => { console.log('Action performed:', notification);});
附件
¥Attachments
附件将媒体内容添加到通知中。支持因平台而异。
¥Attachments add media content to notifications. Support varies by platform.
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', }, ],});
附件属性
¥Attachment Properties
属性 | 描述 |
---|---|
id | 唯一标识符 |
url | Content URL using asset:// or file:// protocol |
注意:在目标平台上测试附件以确保兼容性。
¥Note: Test attachments on your target platforms to ensure compatibility.
通道
¥Channels
通道将通知组织成具有不同行为的类别。虽然主要用于 Android,但它们提供了跨平台的一致 API。
¥Channels organize notifications into categories with different behaviors. While primarily used on Android, they provide a consistent API across platforms.
创建通道
¥Create a Channel
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',});
通道属性
¥Channel Properties
属性 | 描述 |
---|---|
id | 唯一标识符 |
name | 显示名称 |
description | 目的描述 |
importance | 优先级(无、最小、低、默认、高) |
visibility | 隐私设置(秘密、私有、公开) |
lights | 启用通知 LED(Android) |
lightColor | LED 颜色 (Android) |
vibration | 启用振动 |
sound | 自定义声音文件名 |
管理通道
¥Managing Channels
列出现有通道:
¥List existing channels:
import { channels } from '@tauri-apps/plugin-notification';
const existingChannels = await channels();
删除通道:
¥Remove a channel:
import { removeChannel } from '@tauri-apps/plugin-notification';
await removeChannel('messages');
使用通道
¥Using Channels
使用通道发送通知:
¥Send a notification using a channel:
import { sendNotification } from '@tauri-apps/plugin-notification';
sendNotification({ title: 'New Message', body: 'You have a new message', channelId: 'messages',});
注意:在发送引用它们的通知之前创建通道。无效的通道 ID 会阻止显示通知。
¥Note: Create channels before sending notifications that reference them. Invalid channel IDs prevent notifications from displaying.
安全注意事项
¥Security Considerations
除了正常的用户输入清理程序外,目前没有已知的安全注意事项。
¥Aside from normal sanitization procedures of user input there are currently no known security considerations.
Default Permission
This permission set configures which notification features are by default exposed.
Granted Permissions
It allows all notification related features.
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
Permission Table
Identifier | Description |
---|---|
|
Enables the batch command without any pre-configured scope. |
|
Denies the batch command without any pre-configured scope. |
|
Enables the cancel command without any pre-configured scope. |
|
Denies the cancel command without any pre-configured scope. |
|
Enables the check_permissions command without any pre-configured scope. |
|
Denies the check_permissions command without any pre-configured scope. |
|
Enables the create_channel command without any pre-configured scope. |
|
Denies the create_channel command without any pre-configured scope. |
|
Enables the delete_channel command without any pre-configured scope. |
|
Denies the delete_channel command without any pre-configured scope. |
|
Enables the get_active command without any pre-configured scope. |
|
Denies the get_active command without any pre-configured scope. |
|
Enables the get_pending command without any pre-configured scope. |
|
Denies the get_pending command without any pre-configured scope. |
|
Enables the is_permission_granted command without any pre-configured scope. |
|
Denies the is_permission_granted command without any pre-configured scope. |
|
Enables the list_channels command without any pre-configured scope. |
|
Denies the list_channels command without any pre-configured scope. |
|
Enables the notify command without any pre-configured scope. |
|
Denies the notify command without any pre-configured scope. |
|
Enables the permission_state command without any pre-configured scope. |
|
Denies the permission_state command without any pre-configured scope. |
|
Enables the register_action_types command without any pre-configured scope. |
|
Denies the register_action_types command without any pre-configured scope. |
|
Enables the register_listener command without any pre-configured scope. |
|
Denies the register_listener command without any pre-configured scope. |
|
Enables the remove_active command without any pre-configured scope. |
|
Denies the remove_active command without any pre-configured scope. |
|
Enables the request_permission command without any pre-configured scope. |
|
Denies the request_permission command without any pre-configured scope. |
|
Enables the show command without any pre-configured scope. |
|
Denies the show command without any pre-configured scope. |
Tauri 中文网 - 粤ICP备13048890号