@rescript-tauri/plugin-notification¶
Tauri 2.x 通知プラグイン の ReScript バインディング。トースト通知、スケジュール、チャネル、タップアクションタイプを提供します。@tauri-apps/plugin-notification v2.3.x の安定公開 API を 100% カバーします。
注釈
本パッケージは main ブランチで機能実装が完了しています。初回 npm 公開は 他のパッケージと同タイミングで予定されています。それまでは、ソースリポジトリ経由または workspace link で利用してください。
インストール¶
pnpm add @rescript-tauri/plugin-notification @tauri-apps/plugin-notification
@rescript-tauri/plugin-notification は @rescript-tauri/core と @tauri-apps/plugin-notification の両方を peerDependencies として宣言しているため、上流の各バージョンを呼び出し側で管理できます。
rescript.json の dependencies に本パッケージを追加します:
{
"dependencies": [
"@rescript-tauri/core",
"@rescript-tauri/plugin-notification"
]
}
Rust 側ではプラグイン crate を追加し、builder に登録します:
# src-tauri/Cargo.toml
[dependencies]
tauri-plugin-notification = "2"
// src-tauri/src/main.rs
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_notification::init())
.run(tauri::generate_context!())
.expect("error while running app");
}
Capability 設定¶
Tauri 2.x ではプラグインの権限をすべて明示的に許可する必要があります。通知に必要な最小セットは以下のとおりです:
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"windows": ["main"],
"permissions": [
"core:default",
"notification:default"
]
}
notification:default は本バインディングが公開するすべての通知 API (権限照会 / 送信 / スケジュール / アクションタイプ登録 / pending・active 管理 / Android チャネル管理) をカバーします。
Permission フロー¶
ほとんどのプラットフォームでは、配信が成功する前にユーザーが通知権限を許可する必要があります。sendNotification の呼び出しは必ず権限チェックの後ろに置いてください:
open RescriptTauriPluginNotification
let ensurePermission = async () => {
let granted = await PluginNotification.isPermissionGranted()
if granted {
true
} else {
let perm = await PluginNotification.requestPermission()
perm === #granted
}
}
requestPermission は polymorphic variant [#default | #granted | #denied] (notificationPermission) を返します。#default は #denied と区別されるプラットフォームにおける「未決定」状態を表します。
最小サンプル¶
open RescriptTauriPluginNotification
let granted = await ensurePermission()
if granted {
PluginNotification.sendNotificationText("Hello from rescript-tauri")
PluginNotification.sendNotification({
title: "TAURI",
body: "ReScript bindings work",
})
}
公開 API¶
15 個の通知関数すべてが PluginNotification 配下で公開され、併せて Schedule ファクトリモジュールと Importance / Visibility バリアントモジュールも公開されます:
シンボル |
用途 |
|---|---|
|
権限の照会 |
|
通知を表示します (record 形式 / string 形式) |
|
|
|
Pending (スケジュール済み) 通知の管理 |
|
Active (配信済み) 通知の管理 |
|
Android チャネルの管理 |
|
配信 / タップイベントを購読します ( |
|
|
|
|
|
|
|
|
関連する record / variant 型 (options, attachment, action, actionType, pendingNotification, activeNotification, channel, scheduleInterval, scheduleEvery, removeActiveTarget) はすべて PluginNotification から再エクスポートされています。各フィールドの doc コメントと上流 URL は packages/plugin-notification/src/PluginNotification.resi を参照してください。
Schedule ヘルパー¶
Schedule.t は 3 つのファクトリ経由で生成する opaque 型です:
// Fire once at a specific Date
let _ = PluginNotification.Schedule.at(Date.make(), ~allowWhileIdle=true)
// Fire whenever every present field of the interval matches
let _ = PluginNotification.Schedule.interval({hour: 9, minute: 0})
// Fire every N units of a recurrence kind
let _ = PluginNotification.Schedule.every(#day, ~count=1)
Schedule.every が受け付ける繰り返し種別は #year / #month / #twoWeeks / #week / #day / #hour / #minute / #second です。#second は 上流の iOS では サポートされていません — Schedule.every リファレンス を参照してください。
scheduleInterval の weekday フィールドは上流の規約 1=Sunday … 7=Saturday に従います。
注意点¶
sendNotification overload の分割¶
上流の TypeScript シグネチャは以下です:
sendNotification(options: Options | string): void
ReScript では Options | string 共用体を phantom 型やランタイム分岐なしに表現できません。本バインディングは overload を、引数型を固定した 2 つの関数に分割しています:
PluginNotification.sendNotificationText("Hello") // string form
PluginNotification.sendNotification({title: "Hi"}) // record form
Importance / Visibility バリアント¶
Importance.t と Visibility.t は @as(N) 付きの @unboxed バリアントです。ランタイム上は上流の数値 enum と wire 互換ですが、ReScript からは網羅的にパターンマッチできます:
let importance: PluginNotification.Importance.t = Default // @as(3)
let visibility: PluginNotification.Visibility.t = Private // @as(0)
モジュール |
コンストラクタ |
ワイヤ値 |
|---|---|---|
|
|
|
|
|
|
分岐にはパターンマッチを使います:
switch channel.importance {
| Some(High) => /* loud */
| Some(Low) | Some(Min) | Some(None) => /* quiet */
| _ => /* default */
}
IPC ではなく Web API 経由¶
isPermissionGranted / requestPermission / sendNotification / sendNotificationText は上流で Tauri IPC ではなく DOM の window.Notification Web API 経由でディスパッチされます。実用上の影響は以下のとおりです:
Mocks.mockIPCはこれらの呼び出しをインターセプトできません。ランタイムテストではglobalThis.window.Notification(およびNotification.requestPermission) を直接 stub します。パッケージ内のtests/runtime/plugin_notification.test.mjsを参照してください。core:defaultだけでは不十分です。Tauri の capability 層が Web API 呼び出しを許可するよう、プラグインのnotification:default権限も併せて許可する必要があります。
残りの関数 (pending / cancel / active / registerActionTypes / チャネル API / onNotificationReceived / onAction 等) は通常の Tauri IPC を経由するため、Mocks.mockIPC で検証可能です。
互換性¶
項目 |
対応バージョン |
|---|---|
上流 |
|
Rust |
|
|
|
ReScript |
|
|
|
対応 OS |
Linux / macOS / Windows / iOS / Android |
関連リンク¶
上流ドキュメント: Tauri 2.x 通知プラグイン
上流 JS リファレンス: notification module