通知
使用通知插件向你的用户发送原生通知。
🌐 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.
使用项目的包管理器添加依赖:
npm run tauri add notificationyarn run tauri add notificationpnpm tauri add notificationdeno task tauri add notificationbun tauri add notificationcargo 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
🌐 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:
- 检查是否授予权限
- 如果未授予,则请求权限
- 发送通知
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 | 使用 asset:// 或 file:// 协议的内容 URL |
注意:在你的目标平台上测试附件以确保兼容性。
🌐 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(安卓) |
lightColor | LED 颜色(安卓) |
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.
This default permission set includes the following:
allow-is-permission-grantedallow-request-permissionallow-notifyallow-register-action-typesallow-register-listenerallow-cancelallow-get-pendingallow-remove-activeallow-get-activeallow-check-permissionsallow-showallow-batchallow-list-channelsallow-delete-channelallow-create-channelallow-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号
Nodejs.cn 旗下网站