博主自己制作的插件,目前已经在GitHub上开源,欢迎体验:https://github.com/RUIIIOVO/claude-code-bark-notify

详细描述可以看我的另一篇博客:https://blog.csdn.net/m0_64221589/article/details/160716139?spm=1001.2014.3001.5501

Bark官方文档:https://github.com/Finb/Bark
Bark推送加密文档:https://raw.githubusercontent.com/Finb/Bark/master/docs/encryption.md

目标:
Claude Code 每次结束当前任务后,给 iPhone 发一条 Bark 通知。

博主当前环境是mac+iphone,windows需要修改下文件路径,可以把这篇博客丢给ai,直接让ai帮你配置

通知内容:

  • 标题:项目名
  • 正文:Claude Code 已完成

这样同时开多个 Claude Code 窗口时,也能一眼看出是哪一个项目完成了。

  1. Bark 端准备
  • iPhone 安装 Bark
  • 复制推送地址,格式类似:https://api.day.app/你的key
  • 打开 Bark 的“推送加密”
  • 算法选择 AES-128-CBC
  • 设置一把 16 位的自定义 KEY
  1. Claude Code 脚本
    ~/.claude/claude-stop-bark.sh 写入:
#!/bin/bash
set -euo pipefail

BARK_PUSH_BASE="https://api.day.app/你的key"
BARK_ENCRYPTION_KEY="你自己设置的16位KEY"
BARK_ICON_URL="https://claude.ai/images/claude_app_icon.png"

payload=$(cat)
cwd=$(printf '%s' "$payload" | jq -r '.cwd // ""')
project_name=${cwd##*/}
if [ -z "$project_name" ]; then
  project_name="unknown-project"
fi

export CLAUDE_BARK_PUSH_BASE="$BARK_PUSH_BASE"
export CLAUDE_BARK_PROJECT="$project_name"
export CLAUDE_BARK_ENCRYPTION_KEY="$BARK_ENCRYPTION_KEY"
export CLAUDE_BARK_ICON_URL="$BARK_ICON_URL"

plaintext_json=$(python3 <<'PY'
import json
import os

project = os.environ.get("CLAUDE_BARK_PROJECT", "unknown-project").strip() or "unknown-project"
icon = os.environ.get("CLAUDE_BARK_ICON_URL", "").strip()
print(json.dumps({
    "title": project,
    "body": "Claude Code 已完成",
    "group": "Claude Code",
    "level": "active",
    "isArchive": "1",
    "icon": icon,
}, ensure_ascii=False, separators=(",", ":")))
PY
)

initialization_vector=$(python3 <<'PY'
import secrets
import string
alphabet = string.ascii_letters + string.digits
print(''.join(secrets.choice(alphabet) for _ in range(16)))
PY
)

key_hex=$(printf '%s' "$BARK_ENCRYPTION_KEY" | xxd -ps -c 200)
initialization_vector_hex=$(printf '%s' "$initialization_vector" | xxd -ps -c 200)
ciphertext=$(printf '%s' "$plaintext_json" | openssl enc -aes-128-cbc -K "$key_hex" -iv "$initialization_vector_hex" | base64 | tr -d '\n')

curl -sS --data-urlencode "ciphertext=$ciphertext" --data-urlencode "iv=$initialization_vector" "$BARK_PUSH_BASE"

写完后执行:

chmod +x ~/.claude/claude-stop-bark.sh
  1. Claude Code 配置
    ~/.claude/settings.json 里加上 Stop hook:
{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "/bin/bash /Users/你的用户名/.claude/claude-stop-bark.sh",
            "async": true
          }
        ]
      }
    ]
  }
}
  1. 测试
    手动发一条测试通知:
printf '%s' '{"cwd":"/Users/你的用户名/Documents/Code/项目名"}' | /bin/bash ~/.claude/claude-stop-bark.sh

如果 Bark 收到:

  • 标题:项目名
  • 正文:Claude Code 已完成

说明配置成功。

  1. 如果 Bark 提示解密失败
    通常是 Bark App 里的加密配置和脚本不一致。

iPhone 上这样检查:

  • 打开 Bark
  • 进入“推送加密”
  • 算法选择 AES128
  • 模式选择 CBC
  • Padding 选择 pkcs7
  • Key 填和脚本里完全一致的 16 位字符串
  • IV 先随便填一个 16 位字符串,例如:1234567890123456

说明:

  • Bark 会要求先保存一套本地加密配置
  • 真正解密时,会优先使用请求里带过去的 iv
  • 所以本地 IV 只是为了让配置保存通过
  • 只要 AlgorithmModePaddingKey 一致,就能正常解密
Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐