Notifications(通知)
Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。
「notification(通知)」プラグインを使用して、ユーザーにネイティブ通知を送信します。
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 |
はじめに、「notifications」プラグインをインストールしてください。
自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:
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 で「noficication(通知)」を使用する場合は、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
以下に「notification」プラグインの使用方法の例をいくつか示します:
- ユーザーへ Notification(通知)を送信
- Notification(通知)にアクションを追加
- Notification(通知)に添付ファイルを追加
- 特定のチャンネルで Notification(通知)を送信
「notification」プラグインは JavaScript と Rust の両方で利用できます。
「通知 notification」を送信するには、次の手順に従ってください:
-
アクセス権限が与えられているかどうかを確認します
-
アクセス権限が与えられていない場合はその許可を要求します
-
「通知」を送信します
import { isPermissionGranted, requestPermission, sendNotification,} from '@tauri-apps/plugin-notification';// `"withGlobalTauri": true` を使用する場合は、// const { isPermissionGranted, requestPermission, sendNotification, } = window.__TAURI__.notification; を使用できます
// 通知を送信するためのアクセス権限はありますか?let permissionGranted = await isPermissionGranted();
// アクセス権限が設定されていない場合はアクセス権限を要求する必要がありますif (!permissionGranted) { const permission = await requestPermission(); permissionGranted = permission === 'granted';}
// アクセス権限が付与され次第、通知が送信されますif (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(アクション)は、notification(通知)にインタラクティブ(対話型)のボタンや入力機能を追加します。これらを使用することで、ユーザーにとって応答性のよい使用感を実現できます。
インタラクティブな要素を定義するには、アクション・タイプを登録します:
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 | 入力フィールド用の表示テキスト |
「通知 notification」アクションでのユーザーの対話型操作の状態を監視検知します:
import { onAction } from '@tauri-apps/plugin-notification';
await onAction((notification) => { console.log('Action performed:', notification);});添付ファイルは「通知 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 |
注: 添付ファイルがターゲット・プラットフォームで利用できるかどうかの確認を行なってください。
「チャンネル」は、notification(通知)を異なる動作ごとに分類するものです。主に 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 | notification(通知)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');チャンネルを使用して notification(通知)を送信します:
import { sendNotification } from '@tauri-apps/plugin-notification';
sendNotification({ title: 'New Message', body: 'You have a new message', channelId: 'messages',});注: notification(通知)を送信する前に、その通知が参照するチャンネルを作成してください。無効なチャンネル ID の場合、通知は表示されません。
ユーザー入力の通常の安全化手順(機密情報の削除)以外には、現在のところセキュリティに関して考慮すべき事柄はありません。
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. |
【※ この日本語版は、「Nov 10, 2024 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT