Skip to content
Tauri 中文网

通知

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

¥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

使用

¥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:

  1. 检查是否授予权限

    ¥Check if permission is granted

  2. 如果未授予,则请求权限

    ¥Request permission if not granted

  3. 发送通知

    ¥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 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!' });
}

操作

¥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唯一标识符
urlContent 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)
lightColorLED 颜色 (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

notification:allow-batch

Enables the batch command without any pre-configured scope.

notification:deny-batch

Denies the batch command without any pre-configured scope.

notification:allow-cancel

Enables the cancel command without any pre-configured scope.

notification:deny-cancel

Denies the cancel command without any pre-configured scope.

notification:allow-check-permissions

Enables the check_permissions command without any pre-configured scope.

notification:deny-check-permissions

Denies the check_permissions command without any pre-configured scope.

notification:allow-create-channel

Enables the create_channel command without any pre-configured scope.

notification:deny-create-channel

Denies the create_channel command without any pre-configured scope.

notification:allow-delete-channel

Enables the delete_channel command without any pre-configured scope.

notification:deny-delete-channel

Denies the delete_channel command without any pre-configured scope.

notification:allow-get-active

Enables the get_active command without any pre-configured scope.

notification:deny-get-active

Denies the get_active command without any pre-configured scope.

notification:allow-get-pending

Enables the get_pending command without any pre-configured scope.

notification:deny-get-pending

Denies the get_pending command without any pre-configured scope.

notification:allow-is-permission-granted

Enables the is_permission_granted command without any pre-configured scope.

notification:deny-is-permission-granted

Denies the is_permission_granted command without any pre-configured scope.

notification:allow-list-channels

Enables the list_channels command without any pre-configured scope.

notification:deny-list-channels

Denies the list_channels command without any pre-configured scope.

notification:allow-notify

Enables the notify command without any pre-configured scope.

notification:deny-notify

Denies the notify command without any pre-configured scope.

notification:allow-permission-state

Enables the permission_state command without any pre-configured scope.

notification:deny-permission-state

Denies the permission_state command without any pre-configured scope.

notification:allow-register-action-types

Enables the register_action_types command without any pre-configured scope.

notification:deny-register-action-types

Denies the register_action_types command without any pre-configured scope.

notification:allow-register-listener

Enables the register_listener command without any pre-configured scope.

notification:deny-register-listener

Denies the register_listener command without any pre-configured scope.

notification:allow-remove-active

Enables the remove_active command without any pre-configured scope.

notification:deny-remove-active

Denies the remove_active command without any pre-configured scope.

notification:allow-request-permission

Enables the request_permission command without any pre-configured scope.

notification:deny-request-permission

Denies the request_permission command without any pre-configured scope.

notification:allow-show

Enables the show command without any pre-configured scope.

notification:deny-show

Denies the show command without any pre-configured scope.


Tauri 中文网 - 粤ICP备13048890号