🔊

WindowsでClaude CodeのHooksを使って通知音を鳴らす

に公開

1.はじめに

Claude Code には Hooks と呼ばれる「Claude Code が特定の状態になったときに外部処理を実行するための仕組み」が存在します。

今回は Hooks を使って、下記2パターンの場合に通知音を鳴らすように設定します。

  • Claude Code によるセッションが完了した時に通知音を鳴らす
  • Claude Code がユーザからの操作や承認を待つ際に通知音を鳴らす

※環境は非WSL環境のWindowsVSCode上でのClaude Code使用を前提とします。
(今更感はある内容ですが・・・個人的にはWindowsではこれが決定版だと思ってます)

■ settings.json

設定ファイルだけ欲しい方向けにsettings.jsonを掲載しておきます。

{
  "hooks": {
    "Notification": [
      {
        "matcher": "permission_prompt",
        "hooks": [
          {
            "type": "command",
            "command": "powershell -ExecutionPolicy RemoteSigned -Command \"Import-Module BurntToast; New-BurntToastNotification -Text 'ClaudeCode', 'Claude Code has requested confirmation' -Sound Default\""
          }
        ]
      }
    ],
    "Stop": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "powershell -ExecutionPolicy RemoteSigned -Command \"Import-Module BurntToast; New-BurntToastNotification -Text 'ClaudeCode', 'The process is complete' -Sound Default\""
          }
        ]
      }
    ]
  }
}

■ 前提作業:通知音を鳴らすための環境作り

通知音を鳴らすための準備を行います。
まずはPowerShell単体で通知音を鳴らす環境を作成します。

管理者権限でPowerShellを起動します。
下記コマンドで「BurntToast」モジュールをインストールします。

Install-Module -Name BurntToast -Force

その後、下記コマンドを実行し、通知音+通知のポップアップが表示されれば準備完了です。
この後、このコマンドをClaude CodeでのHooksで呼び出す設定を行います。

Import-Module BurntToast; New-BurntToastNotification -Text 'ClaudeCode', 'The process is complete' -Sound Default             

2.Hooksの設定

ここからClaude Code上でHooksの設定を行っていきます。
Hooksの詳細については下記ドキュメントをご確認ください。
https://code.claude.com/docs/en/hooks

■ 処理完了時の通知設定:Stop

Stop では「Claude Code によるセッションが完了した時の通知設定」を行います。

Claude Code を起動し、「/hooks」コマンドを実行します。
下記画面が表示されるので、「6.Stop」を選択します。

Select hook event:                                                                                                         │
│ ↑ 2.  PostToolUse - After tool execution                                                                                   │
│   3.  Notification - When notifications are sent                                                                           │
│   4.  UserPromptSubmit - When the user submits a prompt                                                                    │
│   5.  SessionStart - When a new session is started                                                                         │
│ ❯ 6.  Stop - Right before Claude concludes its response   

続いて表示された画面で「Add new hook」を選択し、「Command:」に下記コマンドを設定します。

powershell -ExecutionPolicy RemoteSigned -Command "Import-Module BurntToast; New-BurntToastNotification -Text 'ClaudeCode', 'The process is complete' -Sound Default"


settings.json

今回の設定をJSONで記述する場合は下記のようになります。

{
  "hooks": {
    "Stop": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "powershell -ExecutionPolicy RemoteSigned -Command \"Import-Module BurntToast; New-BurntToastNotification -Text 'ClaudeCode', 'The process is complete' -Sound Default\""
          }
        ]
      }
    ]
  }
}

■ ユーザからの応答を待つ際の通知設定:Notification

Notification では「Claude Codeがユーザからの操作や承認を待つ際の通知設定」を行います。

公式ドキュメントを見ると、
通知を出すタイミング(matcher)について下記が用意されています。
今回は、「permission_prompt:Codeからの権限リクエスト」を対象にHooksを設定します。

matcher名 説明
permission_prompt Claude Codeからの権限リクエスト
idle_prompt Claudeがユーザー入力を待っているとき(60+秒のアイドル時間後)
auth_success 認証成功通知
elicitation_dialog Claude CodeがMCPツールの引き出しに入力を必要とする場合

Claude Code を起動し、「/hooks」コマンドを実行します。
下記画面が表示されるので、「3.Notification」を選択します。

│ Select hook event:                                                                                                         │
│   1.  PreToolUse - Before tool execution                                                                                   │
│   2.  PostToolUse - After tool execution                                                                                   │
│ ❯ 3.  Notification - When notifications are sent 

続いて表示された画面で「Add new hook」を選択し、「Tool matcher:」は「permission_prompt」を設定します。

続いて「Command:」にはStopと同様のコマンドを設定します(メッセージだけ変更)。

powershell -ExecutionPolicy RemoteSigned -Command "Import-Module BurntToast; New-BurntToastNotification -Text 'ClaudeCode', 'Claude Code has requested confirmation' -Sound Default"

settings.json

今回の設定をJSONで記述する場合は下記のようになります。

{
  "hooks": {
    "Notification": [
      {
        "matcher": "permission_prompt",
        "hooks": [
          {
            "type": "command",
            "command": "powershell -ExecutionPolicy RemoteSigned -Command \"Import-Module BurntToast; New-BurntToastNotification -Text 'ClaudeCode', 'Claude Code has requested confirmation' -Sound Default\""
          }
        ]
      }
    ]
  }
}

3.動作確認してみる

簡単なプロンプトで動作確認してみます。

■ セッション完了の通知(Stop)

セッション完了後に通知音+ポップアップが表示されました。

■ ユーザからの応答を待つ際の通知(Notification)

WEB検索するように指示したところ、
WEB検索実行の許可を求めてきたタイミングで通知音+ポップアップが表示されました。

Discussion