@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.jsondependencies に本パッケージを追加します:

{
  "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 バリアントモジュールも公開されます:

シンボル

用途

isPermissionGranted / requestPermission

権限の照会

sendNotification / sendNotificationText

通知を表示します (record 形式 / string 形式)

registerActionTypes

options.actionTypeId から参照されるタップアクションを宣言します

pending / cancel / cancelAll

Pending (スケジュール済み) 通知の管理

active / removeActive / removeAllActive

Active (配信済み) 通知の管理

createChannel / removeChannel / channels

Android チャネルの管理

onNotificationReceived / onAction

配信 / タップイベントを購読します (Core.PluginListener.t を返します)

Schedule.at / Schedule.interval / Schedule.every

options.schedule 用の Schedule.t を生成します

Importance.{None, Min, Low, Default, High}

@unboxed バリアント: Android チャネルの importance (@as(0..4))

Visibility.{Secret, Private, Public}

@unboxed バリアント: Android チャネルの visibility (@as(-1..1))

notificationPermission

[#default | #granted | #denied]

関連する 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 リファレンス を参照してください。

scheduleIntervalweekday フィールドは上流の規約 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.tVisibility.t@as(N) 付きの @unboxed バリアントです。ランタイム上は上流の数値 enum と wire 互換ですが、ReScript からは網羅的にパターンマッチできます:

let importance: PluginNotification.Importance.t = Default  // @as(3)
let visibility: PluginNotification.Visibility.t = Private  // @as(0)

モジュール

コンストラクタ

ワイヤ値

Importance

None / Min / Low / Default / High

0 / 1 / 2 / 3 / 4

Visibility

Secret / Private / Public

-1 / 0 / 1

分岐にはパターンマッチを使います:

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 で検証可能です。

互換性

項目

対応バージョン

上流 @tauri-apps/plugin-notification

^2.3.0 (peer)

Rust tauri-plugin-notification

2.x

@rescript-tauri/core

^0.1.0 (peer)

ReScript

>=12.0.0

@rescript/core

>=1.6.0

対応 OS

Linux / macOS / Windows / iOS / Android

関連リンク