コンテンツにスキップ
Tauri

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 notification

以下に「notification」プラグインの使用方法の例をいくつか示します:

「notification」プラグインは JavaScript と Rust の両方で利用できます。

「通知 notification」を送信するには、次の手順に従ってください:

  1. アクセス権限が与えられているかどうかを確認します

  2. アクセス権限が与えられていない場合はその許可を要求します

  3. 「通知」を送信します

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

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トリガーされるとアプリを前面に配置
destructiveiOS ではアクションが赤で表示
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一意の識別子
urlasset:// または 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プライバシー設定(秘密、非公開、公開)
lightsnotification(通知)LED の有効化(Android)   
lightColorLED の色(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-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.

【※ この日本語版は、「Nov 10, 2024 英語版」に基づいています】


© 2025 Tauri Contributors. CC-BY / MIT