4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Claude CodeのHooks機能でWindows通知を実装する

4
Last updated at Posted at 2026-01-25

目的

Claude Codeを使っていると、長い処理の完了に気づかないことがあります。
また、Claudeがユーザーに確認を求めているのに気づかず放置してしまうことも。。。

そこで、Claude CodeのHooks機能を使って、処理完了時やユーザー入力待ち時にWindowsの通知を表示させてみます。

前提

  • WSL2 (Ubuntu) 環境
  • Claude Code インストール済み

Hooks とは

Claude Code の Hooks は、特定のイベント発生時にカスタムスクリプトを実行できる機能です。

今回使用するイベントは以下の2つです。

イベント 発火タイミング
Stop Claudeが回答生成を完了し、動作が停止したとき
Notification Claudeがユーザーへの入力を求めるときなど

実装

(補足)Windows通知の仕組み

WSLからWindows側のPowerShellを呼び出し、Windows Runtime APIを使って通知を表示します。

最小構成は以下のようになります。

stop.sh
#!/bin/bash

powershell.exe -Command "
    [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
    [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
    \$template = '<toast><visual><binding template=\"ToastText02\"><text id=\"1\">タイトル</text><text id=\"2\">メッセージ</text></binding></visual></toast>'
    \$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
    \$xml.LoadXml(\$template)
    \$toast = [Windows.UI.Notifications.ToastNotification]::new(\$xml)
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('Claude Code').Show(\$toast)
"
コードの解説

コードの解説

説明
powershell.exe -Command WSLからWindows側のPowerShellを呼び出し
[Windows.UI.Notifications.ToastNotificationManager...] 通知管理クラスをロード
[Windows.Data.Xml.Dom.XmlDocument...] XMLパーサークラスをロード
\$template = '<toast>...</toast>' 通知のXMLテンプレートを定義
\$xml.LoadXml(\$template) XMLをパース
[...ToastNotification]::new(\$xml) 通知オブジェクトを生成
[...ToastNotificationManager]::CreateToastNotifier('Claude Code').Show(\$toast) 通知を表示

XMLテンプレートの構造

<toast>
    <visual>
        <binding template="ToastText02">
            <text id="1">タイトル(太字)</text>
            <text id="2">本文</text>
        </binding>
    </visual>
</toast>

ToastText02テンプレートはタイトル+本文の2行構成です。

処理完了通知スクリプト(Stop Hook)

Claude Codeが処理を完了したときに通知するスクリプトを作成します。

~/.claude/scripts/stop.sh
#!/bin/bash
# Claude Code - 回答完了通知
# Hook設定: PostResponse で呼び出し

TIMESTAMP=$(date +%H:%M:%S)

PROJECT_NAME=$(basename "${CLAUDE_PROJECT_DIR}")

powershell.exe -Command "
    [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
    [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
    \$template = '<toast><visual><binding template=\"ToastText02\"><text id=\"1\">✅ ${PROJECT_NAME}</text><text id=\"2\">回答の生成が完了しました ($TIMESTAMP)</text></binding></visual><audio silent=\"true\"/></toast>'
    \$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
    \$xml.LoadXml(\$template)
    \$toast = [Windows.UI.Notifications.ToastNotification]::new(\$xml)
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('Claude Code').Show(\$toast)
"

powershell.exe -c '(New-Object Media.SoundPlayer "C:\Windows\Media\Windows Ding.wav").PlaySync()'

特徴
CLAUDE_PROJECT_DIR環境変数を使い、プロジェクト名を通知に含める様にしています。
<audio silent="true"/>PlaySync()を使い、完了音をカスタマイズしています。

入力待ち通知スクリプト(Notification Hook)

Claudeがユーザーの入力や許可を待っているときに通知するスクリプトです。

~/.claude/scripts/notification.sh
#!/bin/bash
# Claude Code - 許可リクエスト / 入力待ち通知
# Hook設定: Notification で呼び出し

TIMESTAMP=$(date +%H:%M:%S)

PROJECT_NAME=$(basename "${CLAUDE_PROJECT_DIR}")

powershell.exe -Command "
    [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
    [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
    \$template = '<toast><visual><binding template=\"ToastText02\"><text id=\"1\">⏳ ${PROJECT_NAME}</text><text id=\"2\">入力を待っています ($TIMESTAMP)</text></binding></visual><audio silent=\"true\"/></toast>'
    \$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
    \$xml.LoadXml(\$template)
    \$toast = [Windows.UI.Notifications.ToastNotification]::new(\$xml)
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('Claude Code').Show(\$toast)
"

powershell.exe -c '(New-Object Media.SoundPlayer "C:\Windows\Media\Windows Ding.wav").PlaySync()'

特徴
CLAUDE_PROJECT_DIR環境変数を使い、プロジェクト名を通知に含める様にしています。
<audio silent="true"/>PlaySync()を使い、通知音をカスタマイズしています。

settings.json の設定

方法①または方法②お好きなほうで大丈夫です。

方法①: 直接編集

スクリプトを作成したら、Claude Codeの設定ファイルにHooksを追加します。
"hooks"部分が今回の編集対象箇所になります。

~/.claude/settings.json
{
  "$schema": "https://json.schemastore.org/claude-code-settings.json",
  "permissions": {
    "allow": []
  },
  "hooks": {
    "Stop": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/scripts/stop.sh"
          }
        ]
      }
    ],
    "Notification": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/scripts/notification.sh"
          }
        ]
      }
    ]
  },
  "feedbackSurveyState": {
    "lastShownTime": 1754154357139
  }
}

方法②: Claude Code から編集

まずはUbuntuターミナルから適当なプロジェクト上でClaude Codeを立ち上げましょう。

Ubuntu
$ claude

Claude Code上で/hooksコマンドを実行してください。
そうすると設定するhooks一覧が表示されますので、Stopを選択します。

image.png

+ Add new hook...を選択

image.png

hookのコマンドを求められますので、~/.claude/scripts/stop.shを入力します。
※スクリプトファイルを別の場所に作成した場合はそのパスを使ってください。

image.png

設定範囲を指定します。
今回はClaude Code全体で動作してほしいので、User settingsを設定します。

image.png

設定後、再度hooksコマンドを実行してStopの中身を確認すると、
先ほど設定したhookが追加されていることが確認できます。
この時点で~/.claude/settings.jsonにも設定が反映されているはずです。

image.png

Notification hook も Stop hook と同じように設定しましょう。
コマンド → ~/.claude/scripts/notification.sh

動作確認

hooksの設定が完了したら、動作確認をしましょう。

今週の献立を考えてください。好みについては聞いてください。

image.png

回答を生成するためにユーザーに追加の質問をしてきます。
その際にNotification hookの通知が飛んでくれば成功です。

image.png

image.png

回答生成完了後、Stop hookの通知が飛ぶことまで確認できます。

image.png

通知センターにもしっかりと出ていますね。

image.png

まとめ

Claude CodeのHooks機能を使うことで、処理完了や入力待ちの通知をWindows側に表示できるようになりました。
これで長時間の処理を待っている間に別作業をしていても、完了を見逃すことがなくなりますね。

Hooksには他にも PreToolUsePostToolUseSessionStart など様々なイベントがあるので、自分のワークフローに合わせてカスタマイズしてみてください。

告知

2026年2月5日(木)に ARI TechSummit ~AI・生成AI~ を開催いたします。

ARI が主催する AWS・クラウド×生成AI・AIエージェント をテーマとしたオフラインLT大会です。
現在参加者を絶賛募集中ですので、ぜひお越しください!

日時
2026年2月5日(木)19:00 - 21:45
(開場・受付開始:18:30)

テーマ
「AI・生成AI」をテーマに活用・事例共有を行います。
(前半:ライトニングトーク、後半:懇親会)

場所
ARアドバンストテクノロジ株式会社 (https://ari-jp.com) 本社オフィス
〒 150-0002 東京都渋谷区渋谷2-17-1 渋谷アクシュ18F

参加費
無料

お申し込み方法
connpassのイベントページからお申込みください。
※申し込み締切:2026年2月5日 15:00

タイムテーブル(予定)

時間 内容 登壇者(敬称略)
18:30 - 19:00 開場・受付開始 -
19:00 - 19:10 オープニング 司会
19:10 - 19:20 仮)AWSのBedrockを導入する現場の話 ARアドバンストテクノロジ株式会社
鈴木 健一 
19:20 - 19:30 長期記憶を持ったAIエージェントデモンストレーション ARアドバンストテクノロジ株式会社
野口 栄司
19:30 - 20:00 フルサーバーレスなAIエージェントをCDKでサクッと作ろう!
(Amplify & AgentCore構築ライブデモ)
KDDIアジャイル開発センター株式会社
御田 稔(みのるん)
20:00 - 20:10 休憩 -
20:10 - 20:20 Kiroに入門!spec駆動開発の概念と使い方を知ろう 大畑 一志
20:20 - 20:30 Amazon QuickとAIエージェント ARアドバンストテクノロジ株式会社
岡村 紘希
20:30 - 20:45 仮) AI エージェントの賢い使い方 アマゾン ウェブ サービス ジャパン合同会社
九曜 克之
20:45 - 21:00 仮) 物語でAIを学ぶ
AWS Bedrockデモを交えて楽しく学びます
Udemy講師 ARアドバンストテクノロジ株式会社
木村 利広
21:00 - 21:45 懇親会 ARアドバンストテクノロジ株式会社
4
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?