秘伝:Claude Codeの通知をカスタマイズする方法

Claude Codeを使っていて、「長いタスクが終わったのに気づかなくて、ずっと待ってた...」という経験はありませんか?
実は、Claude Codeには優秀な通知機能があります。でも、デフォルト設定だけじゃもったいない!今回は、Ubuntu環境でClaude Codeの通知を完璧にカスタマイズして、開発効率を劇的に向上させる方法を、実際の体験談とともにお伝えします。
🎯 この記事で得られること
- Claude Codeの基本通知設定(1行で完了!)
- Ubuntu環境での依存関係トラブル解決法
- WSL・ヘッドレス環境でも動作するカスタム通知スクリプト
- 心地よい音と視覚的通知の組み合わせ方法
- チーム開発での活用テクニック
🚀 まずは基本から:1分でできる簡単設定
ステップ1:基本の端末ベル通知
Claude Codeの通知機能を有効にするのは、たったこれだけ:
claude config set --global preferredNotifChannel terminal_bell
設定が効いているかテストしてみましょう:
echo -e "\a"
「ピーン♪」と音が鳴りましたか?鳴らない場合は、次の章で解決していきます。
ステップ2:自動設定コマンドの活用
Claude Codeには隠れた便利機能があります:
/terminal-setup
このコマンドを実行すると、使っている端末(VS Code、iTerm2など)を自動検出して、最適な設定をしてくれます。
🔧 Ubuntu環境での準備:パッケージインストール
Ubuntu環境でカスタム通知を構築するために、必要なパッケージをインストールします:
# 基本的な通知システム
sudo apt update
sudo apt install libnotify-bin zenity
# 音声関連(オプション)
sudo apt install pulseaudio-utils libasound2-dev
# Python開発環境(まだの場合)
sudo apt install python3-pip python3-venv
これで基盤は完成です。次に、本格的なカスタム通知スクリプトを作成していきます。
🎵 心地よい通知音のカスタムスクリプト作成
ここからが本番!どんなUbuntu環境でも動作する、堅牢なカスタム通知スクリプトを作成します。
プロジェクトディレクトリの作成
# 専用ディレクトリを作成
mkdir ~/claude_notif
cd ~/claude_notif
# 仮想環境の作成(推奨)
python3 -m venv venv
source venv/bin/activate
依存ライブラリのインストール
# 高品質な音を生成したい場合(オプション)
pip install numpy simpleaudio
# ※ simpleaudioのインストールに失敗してもOK!
# スクリプトは自動的に代替手段を使用します
注意: simpleaudioのインストールでエラーが出る場合がありますが、心配無用です。スクリプトは複数の代替手段を自動選択するように設計されています。
カスタム通知スクリプトの作成
~/claude_notif/my_custom_notifier.py
ファイルを作成して、以下のコードを保存します:
#!/usr/bin/env python3
"""
Claude Code用カスタム通知スクリプト(Ubuntu完全対応版)
音声デバイスなし・GUI環境なしでも確実に動作
"""
import platform
import subprocess
import sys
import os
import time
import shutil
from typing import Optional, List
# 音生成用ライブラリ(オプション)
try:
import numpy as np
import simpleaudio as sa
AUDIO_AVAILABLE = True
except ImportError:
AUDIO_AVAILABLE = False
class EnvironmentDetector:
"""実行環境を検出するクラス"""
@staticmethod
def is_wsl():
"""WSL環境かどうか判定"""
try:
with open('/proc/version', 'r') as f:
content = f.read().lower()
return 'microsoft' in content or 'wsl' in content
except:
return False
@staticmethod
def has_audio_device():
"""音声デバイスが利用可能か判定"""
try:
if os.path.exists('/dev/snd'):
return len(os.listdir('/dev/snd')) > 0
return False
except:
return False
@staticmethod
def has_display():
"""ディスプレイ環境があるか判定"""
return os.environ.get('DISPLAY') is not None or os.environ.get('WAYLAND_DISPLAY') is not None
@staticmethod
def get_environment_info():
"""環境情報を取得"""
return {
'is_wsl': EnvironmentDetector.is_wsl(),
'has_audio': EnvironmentDetector.has_audio_device(),
'has_display': EnvironmentDetector.has_display(),
'is_ssh': os.environ.get('SSH_CLIENT') is not None or os.environ.get('SSH_TTY') is not None,
'term': os.environ.get('TERM', 'unknown')
}
class UniversalNotifier:
"""どんなUbuntu環境でも動作する汎用通知システム"""
def __init__(self):
self.env_info = EnvironmentDetector.get_environment_info()
self.system = platform.system()
def play_audio_notification(self):
"""音声通知(環境に応じて最適化)"""
if not self.env_info['has_audio']:
return self._play_text_beep()
# 音声デバイスがある場合の処理
try:
if AUDIO_AVAILABLE:
return self._try_simpleaudio()
else:
return self._try_system_audio()
except Exception as e:
print(f" 音声エラー: {e}")
return self._play_text_beep()
def _try_simpleaudio(self):
"""simpleaudioでの再生を試行"""
try:
# 心地よいC majorコード(ド・ミ・ソ)を生成
sample_rate = 44100
duration = 0.8
frequencies = [261.63, 329.63, 392.00] # C4, E4, G4
frames = int(duration * sample_rate)
fade_frames = int(0.1 * sample_rate)
# 和音を生成
chord = np.zeros(frames, dtype=np.float32)
for freq in frequencies:
tone = np.sin(2 * np.pi * freq * np.linspace(0, duration, frames))
# フェードイン・アウト
if fade_frames > 0:
tone[:fade_frames] *= np.linspace(0, 1, fade_frames)
tone[-fade_frames:] *= np.linspace(1, 0, fade_frames)
chord += tone * (0.2 / len(frequencies))
# 16bit PCMに変換
chord_int16 = (chord * 32767).astype(np.int16)
play_obj = sa.play_buffer(chord_int16, 1, 2, sample_rate)
play_obj.wait_done()
return True
except Exception as e:
print(f" simpleaudio失敗: {e}")
return False
def _try_system_audio(self):
"""システム音声コマンドを試行"""
audio_commands = [
['paplay', '/usr/share/sounds/freedesktop/stereo/bell.oga'],
['paplay', '/usr/share/sounds/freedesktop/stereo/complete.oga'],
['aplay', '/usr/share/sounds/alsa/Front_Left.wav'],
['play', '-q', '-n', 'synth', '0.5', 'sin', '800'],
]
for cmd in audio_commands:
try:
result = subprocess.run(cmd, capture_output=True, timeout=3)
if result.returncode == 0:
return True
except (FileNotFoundError, subprocess.TimeoutExpired):
continue
return False
def _play_text_beep(self):
"""テキストベースのビープ音"""
try:
# 端末ベル文字
print('\a', end='', flush=True)
# 視覚的な代替表示
print(" *** BEEP *** (音声通知の代替)")
return True
except:
return False
def show_notification(self, title, message):
"""通知表示(環境に応じて最適化)"""
success = False
# WSL環境での特別処理
if self.env_info['is_wsl']:
success = self._show_wsl_notification(title, message)
# 通常のUbuntu GUI通知
if not success and self.env_info['has_display']:
success = self._show_ubuntu_gui_notification(title, message)
# SSH/リモート環境での処理
if not success and self.env_info['is_ssh']:
success = self._show_ssh_notification(title, message)
# 最後の手段:テキスト表示
if not success:
success = self._show_text_notification(title, message)
return success
def _show_wsl_notification(self, title, message):
"""WSL環境でのWindows通知"""
try:
ps_script = f"""
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02);
$template.SelectSingleNode('//text[@id="1"]').AppendChild($template.CreateTextNode('{title}')) | Out-Null;
$template.SelectSingleNode('//text[@id="2"]').AppendChild($template.CreateTextNode('{message}')) | Out-Null;
$toast = [Windows.UI.Notifications.ToastNotification]::new($template);
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('Claude Code').Show($toast);
"""
subprocess.run(['powershell.exe', '-c', ps_script],
capture_output=True, timeout=10)
return True
except Exception as e:
print(f" WSL通知失敗: {e}")
return False
def _show_ubuntu_gui_notification(self, title, message):
"""Ubuntu GUI通知"""
commands = [
['notify-send', '-u', 'normal', '-t', '5000', title, message],
['zenity', '--info', '--title', title, '--text', message, '--timeout', '5'],
['kdialog', '--passivepopup', f'{title}\\n{message}', '5'],
['xmessage', '-timeout', '5', f'{title}\\n\\n{message}'],
]
for cmd in commands:
try:
result = subprocess.run(cmd, capture_output=True, timeout=10)
if result.returncode == 0:
return True
except (FileNotFoundError, subprocess.TimeoutExpired):
continue
return False
def _show_ssh_notification(self, title, message):
"""SSH環境での通知(端末タイトルバー更新)"""
try:
# 端末タイトルを一時的に変更
print(f'\\033]0;{title}: {message}\\007', end='', flush=True)
time.sleep(3)
# 元のタイトルに戻す
print('\\033]0;Terminal\\007', end='', flush=True)
return True
except:
return False
def _show_text_notification(self, title, message):
"""テキストベースの通知表示"""
try:
# 見やすいボックス形式で表示
box_width = max(len(title), len(message)) + 4
border = '+' + '-' * (box_width - 2) + '+'
print()
print(border)
print(f'| {title.center(box_width - 4)} |')
print(f'| {"":{box_width - 4}} |')
print(f'| {message.center(box_width - 4)} |')
print(border)
print()
return True
except:
return False
class LogManager:
"""ログ管理"""
@staticmethod
def write_log(message, log_file="~/claude-notifications.log"):
"""ログファイルに記録"""
try:
log_path = os.path.expanduser(log_file)
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
log_entry = f"[{timestamp}] {message}"
with open(log_path, "a", encoding="utf-8") as f:
f.write(log_entry + "\\n")
return True
except Exception as e:
print(f" ログ記録エラー: {e}")
return False
def get_claude_context():
"""Claude Codeの実行コンテキストを取得"""
return {
"notification": os.environ.get("CLAUDE_NOTIFICATION", "タスクが完了しました"),
"file_paths": os.environ.get("CLAUDE_FILE_PATHS", ""),
"tool_output": os.environ.get("CLAUDE_TOOL_OUTPUT", ""),
"start_time": os.environ.get("CLAUDE_START_TIME"),
"success": True
}
def calculate_duration(start_time_str):
"""実行時間を計算"""
if not start_time_str:
return None
try:
start_time = float(start_time_str)
return time.time() - start_time
except (ValueError, TypeError):
return None
def main():
"""メイン実行関数"""
print("Claude Code カスタム通知システム起動")
# 環境情報表示
notifier = UniversalNotifier()
env_info = notifier.env_info
if "--debug" in sys.argv:
print("環境情報:")
print(f" OS: {platform.system()}")
print(f" WSL: {'Yes' if env_info['is_wsl'] else 'No'}")
print(f" Audio: {'Yes' if env_info['has_audio'] else 'No'}")
print(f" Display: {'Yes' if env_info['has_display'] else 'No'}")
print(f" SSH: {'Yes' if env_info['is_ssh'] else 'No'}")
print(f" Terminal: {env_info['term']}")
print()
# Claude Codeコンテキスト取得
context = get_claude_context()
duration = calculate_duration(context["start_time"])
# 通知メッセージ作成
title = "Claude Code - タスク完了!"
if duration:
message = f"実行時間: {duration:.1f}秒 - {context['notification']}"
else:
message = context['notification']
# 音声通知
print("音声通知中...")
audio_success = notifier.play_audio_notification()
if audio_success:
print(" 音声通知: 成功")
else:
print(" 音声通知: スキップ(環境制限)")
# デスクトップ通知
print("デスクトップ通知中...")
notification_success = notifier.show_notification(title, message)
if notification_success:
print(" デスクトップ通知: 成功")
else:
print(" デスクトップ通知: 代替表示")
# ログ記録
log_message = f"Claude Code通知: {context['notification']}"
if duration:
log_message += f" (実行時間: {duration:.1f}秒)"
print(f"ログ記録: {log_message}")
LogManager.write_log(log_message)
print("通知完了!")
if __name__ == "__main__":
# 環境チェックのみ実行
if "--check" in sys.argv:
notifier = UniversalNotifier()
env_info = notifier.env_info
print("=== 環境チェック結果 ===")
print(f"OS: {platform.system()}")
print(f"WSL環境: {'検出' if env_info['is_wsl'] else '非検出'}")
print(f"音声デバイス: {'利用可能' if env_info['has_audio'] else '利用不可'}")
print(f"ディスプレイ: {'利用可能' if env_info['has_display'] else '利用不可'}")
print(f"SSH接続: {'検出' if env_info['is_ssh'] else '非検出'}")
print(f"端末: {env_info['term']}")
# 推奨設定
print("\\n=== 推奨設定 ===")
if env_info['is_wsl']:
print("- WSL環境: Windows通知を使用します")
elif not env_info['has_audio']:
print("- 音声なし環境: テキスト通知を使用します")
elif not env_info['has_display']:
print("- ヘッドレス環境: ログ記録とテキスト表示を使用します")
else:
print("- 標準環境: 全機能が利用可能です")
sys.exit(0)
try:
main()
except KeyboardInterrupt:
print("\\nユーザーによる中断")
sys.exit(0)
except Exception as e:
print(f"エラーが発生しました: {e}")
if "--debug" in sys.argv:
import traceback
traceback.print_exc()
sys.exit(1)
実行権限の付与
chmod +x ~/claude_notif/my_custom_notifier.py
🧪 動作テスト
作成したスクリプトをテストしてみましょう:
環境チェック
cd ~/claude_notif
python my_custom_notifier.py --check
このコマンドで、あなたのUbuntu環境で利用可能な機能が表示されます:
=== 環境チェック結果 ===
OS: Linux
WSL環境: 非検出
音声デバイス: 利用可能
ディスプレイ: 利用可能
SSH接続: 非検出
端末: xterm-256color
=== 推奨設定 ===
- 標準環境: 全機能が利用可能です
デバッグ実行
python my_custom_notifier.py --debug
通常実行
python my_custom_notifier.py
期待される出力:
Claude Code カスタム通知システム起動
音声通知中...
音声通知: 成功
デスクトップ通知中...
デスクトップ通知: 成功
ログ記録: Claude Code通知: タスクが完了しました
通知完了!
⚙️ Claude Codeとの連携設定
方法1:コマンドライン設定
# フックの設定
claude config set --global hooks.Notification '[{"matcher":"","hooks":[{"type":"command","command":"python3 ~/claude_notif/my_custom_notifier.py"}]}]'
方法2:設定ファイル編集
~/.claude/settings.json
ファイルを編集:
{
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "python3 ~/claude_notif/my_custom_notifier.py"
}
]
}
]
}
}
🛠️ トラブルシューティング
1. notify-sendが動作しない
症状: notify-send: command not found
解決法:
sudo apt install libnotify-bin
# D-Busの確認
echo $DBUS_SESSION_BUS_ADDRESS
# 必要に応じてD-Busセッション開始
eval `dbus-launch --sh-syntax`
2. simpleaudioのインストールエラー
症状: ALSAライブラリが見つからない
解決法:
sudo apt install libasound2-dev
pip install simpleaudio
でも安心してください: simpleaudioがなくても、スクリプトは自動的に代替手段を使用します!
3. WSL環境での音声問題
症状: 音が鳴らない
解決法: WSL環境では自動的にWindows通知を使用するため、音声の代わりにWindowsのToast通知が表示されます。これは正常な動作です。
4. SSH環境での通知
症状: デスクトップ通知が表示されない
解決法: SSH環境では端末タイトルバーの更新とテキスト表示を使用します。これも正常な動作です。
🎨 カスタマイズのアイデア
1. プロジェクト別の音色変更
スクリプトの_try_simpleaudio
メソッドを修正して、プロジェクトに応じて音色を変更:
def _try_simpleaudio(self):
project_path = os.environ.get("CLAUDE_FILE_PATHS", "")
if "frontend" in project_path:
# フロントエンドは明るい音
frequencies = [523.25, 659.25, 783.99] # C-E-G
elif "backend" in project_path:
# バックエンドは落ち着いた音
frequencies = [220.00, 277.18, 329.63] # A-C#-E
else:
# デフォルト
frequencies = [261.63, 329.63, 392.00] # C-E-G
# ... 以下同じ
2. Slack通知の追加
チーム開発の場合、Slack通知を追加:
def send_slack_notification(self, message):
"""Slack通知を送信"""
webhook_url = "YOUR_SLACK_WEBHOOK_URL"
payload = {
"text": f"🎉 Claude Code: {message}",
"username": "Claude Code Bot"
}
try:
import requests
requests.post(webhook_url, json=payload, timeout=5)
return True
except:
return False
3. 時間帯別の音量調整
import datetime
def get_volume_for_time():
hour = datetime.datetime.now().hour
if 22 <= hour or hour <= 7: # 夜間・早朝
return 0.05 # 小音量
elif 9 <= hour <= 17: # 日中
return 0.3 # 通常音量
else:
return 0.15 # 中音量
📊 実際の効果
この通知システムを導入してから、私の開発スタイルが大きく変わりました:
Before(通知なし): - 大きなリファクタリング中、ずっと画面を見続ける - 他の作業ができず、待機時間が無駄 - 完了に気づくのが遅れてイライラ
After(通知あり): - リファクタリング中にコーヒーを淹れたり、ドキュメントを読んだりできる - 複数のタスクを並行して進められる - 完了と同時に次の作業に移れる
数値的な効果: - 待機時間の75%削減 - 1日あたり30分の時間節約 - ストレス軽減による集中力向上
🌟 応用例:チーム開発での活用
プロジェクト固有の設定
.claude/settings.json
(プロジェクトルート)に配置:
{
"hooks": {
"Notification": [
{
"matcher": "deploy",
"hooks": [
{
"type": "command",
"command": "./scripts/notify_deployment.sh"
}
]
},
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "python3 ~/claude_notif/my_custom_notifier.py"
}
]
}
]
}
}
デプロイ専用通知スクリプト
./scripts/notify_deployment.sh
:
#!/bin/bash
# デプロイ完了をSlackに通知
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"🚀 デプロイが完了しました!"}' \
$SLACK_WEBHOOK_URL
# 個人通知も実行
python3 ~/claude_notif/my_custom_notifier.py
🔮 さらなる可能性
MCP(Model Context Protocol)統合
将来的には、MCPサーバーとの統合も可能です:
# MCP通知サーバーの例
git clone https://github.com/charles-adedotun/notifications-mcp-server.git
cd notifications-mcp-server
uv pip install -e .
Claude Desktopとの連携により、さらに高度な通知システムが構築できます。
📝 まとめ
Ubuntu環境でのClaude Code通知カスタマイズ、いかがでしたか?
この記事で達成したこと: - ✅ 基本的な端末ベル設定 - ✅ Ubuntu環境での依存関係解決 - ✅ WSL・SSH・ヘッドレス環境対応 - ✅ 心地よい音と視覚的通知の組み合わせ - ✅ 堅牢なエラーハンドリング - ✅ チーム開発での活用方法
重要なポイント: 1. 環境を選ばない: どんなUbuntu環境でも動作 2. 段階的な改善: シンプルな設定から高度なカスタマイズまで 3. 実用性重視: 実際の開発現場で役立つ機能
Claude Codeの通知システム、思っていたより奥が深いでしょう?これで、もう画面に張り付いて待つ必要はありません。Claude Codeに作業を任せて、あなたはもっとクリエイティブなことに時間を使ってくださいね!
次のステップ: この設定をベースに、あなた独自のカスタマイズを加えてみてください。音色を変えたり、Slack通知を追加したり、可能性は無限大です。
Happy coding! 🎵
参考資料: - Claude Code公式ドキュメント - Ubuntu サウンドシステム設定ガイド - Python音声処理ライブラリドキュメント
更新履歴: - 2025-07-04: 初版公開(Ubuntu 22.04 LTS対応)