# 任务进度卡片

> 长时间的工作运行时，在锁定屏幕、灵动岛或 Android 状态栏显示一张实时更新的进度卡片。

需要跑一阵子的工作（部署、构建、备份、AI 代理的任务），只在最后发送一条通知往往不够。任务进度卡片会跟着工作的进度更新，做完就结束。

- **iPhone**：锁定屏幕和灵动岛上的实时活动（Live Activities）。
- **Android**：常驻的进度通知。Android 16 起会升级成 Live Update，显示在状态栏的芯片上。

用的是和一般推送同一个推送网址。开一张卡片，想更新几次就更新几次，最后结束它。

```bash
curl "https://opu.sh/YOUR_KEY/部署%20api/构建中?live=start&id=deploy-7&progress=10"
curl "https://opu.sh/YOUR_KEY?live=update&id=deploy-7&progress=60&status=上传中"
curl "https://opu.sh/YOUR_KEY?live=end&id=deploy-7&status=完成"
```

## 开始之前 [#开始之前]

iPhone 要允许 Opush 使用实时活动。如果关掉了，请到“设置”›“Opush”打开，再打开 Opush 一次让它完成注册。设备还不能显示卡片时，`live=start` 会回 `400`。

Apple Watch 不显示任务进度卡片。

## 参数 [#参数]

<TypeTable
  type={{
    live: {
      type: "string",
      description: "start、update 或 end。",
      required: true,
    },
    id: {
      type: "string",
      description: "卡片的名字。start、update、end 都用同一个 id。最多 64 字节。",
      required: true,
    },
    title: {
      type: "string",
      description: "卡片标题。start 时必填，之后不给就沿用。",
    },
    status: {
      type: "string",
      description: "状态文字，例如“正在跑测试”。没有时用 body。",
    },
    progress: {
      type: "number",
      description: "0 到 100。不给就不显示进度条。",
    },
    state: {
      type: "string",
      description: "running（默认）、success 或 failed。end 没给时是 success。",
    },
    alert: {
      type: "string",
      description: "\"1\" 时，这次更新也会亮屏幕提醒。",
    },
  }}
/>

也可以用路径写：`live=start` 时，`/YOUR_KEY/<标题>/<状态>` 会设置标题和状态文字。

- `update` 和 `end` 没给的字段，沿用上一次的值。
- `title` 和 `status` 超过 120 个字会被截短。
- 成功结束（默认）时进度条会补满；用 `state=failed` 结束时，进度条停在原本的位置。

## 什么时候会提醒 [#什么时候会提醒]

大部分的更新都是安静的，只更新卡片，不响也不弹出横幅。

| 推送 | 会提醒吗 |
|---|---|
| `live=start` | 一定会 |
| `state=failed` | 一定会 |
| `alert=1` | 会 |
| 其他的 `update`、`end` | 不会 |

卡片已经在屏幕上时又发送一次 `live=start`，会把内容重新开始并提醒一次，不会再开第二张。

## 响应 [#响应]

| 响应 | 意思 |
|---|---|
| `200`，`message: "success"` | 已发送 |
| `200`，`message: "queued: …"` | 卡片还在设备上启动。Opush 会先记住最新的更新，卡片准备好就发送过去。 |
| `400` | `live` 不是 start、update、end；没有 `id` 或太长；start 没有 `title`；`progress` 不在 0–100；`state` 不对；推送有加密；或设备不能开卡片 |
| `404` | `update` 或 `end` 找不到卡片（没开过、已经结束，或被划掉了） |

## 限制 [#限制]

- iOS 的卡片最多活跃 8 小时，结束后最多在锁定屏幕停留 4 小时。
- Opush 在卡片最后一次更新的 12 小时后清掉记录，之后的更新会回 `404`。
- iOS 会限制卡片更新的频率。Opush 有申请频繁更新，但用户可以在设置里关掉。
- 任务进度卡片的推送和其他推送一样，算进每台设备的推送次数上限。
- 任务进度卡片不能[加密](/zh-Hans/docs/push/encryption)，卡片没办法解密。
- 在 Android 上把卡片划掉，卡片就结束了，之后用同一个 `id` 更新会回 `404`。

## 示例：部署脚本 [#示例部署脚本]

这个脚本把每个步骤显示在卡片上，任何命令失败就把卡片标成失败。Opush 发不出去也不会中断部署。

```bash
#!/usr/bin/env bash
set -euo pipefail

OPUSH="https://opu.sh/YOUR_KEY"
ID="deploy-$(date +%s)"
STEP="准备中"

live() {
  curl -fsS --get "$OPUSH" --data-urlencode "id=$ID" "$@" > /dev/null || true
}

trap 'live --data-urlencode live=end --data-urlencode state=failed --data-urlencode "status=失败：$STEP"' ERR

live --data-urlencode live=start --data-urlencode "title=部署 api" \
     --data-urlencode "status=$STEP" --data-urlencode progress=0

STEP="构建中"
live --data-urlencode live=update --data-urlencode "status=$STEP" --data-urlencode progress=20
npm run build

STEP="测试中"
live --data-urlencode live=update --data-urlencode "status=$STEP" --data-urlencode progress=50
npm test

STEP="上传中"
live --data-urlencode live=update --data-urlencode "status=$STEP" --data-urlencode progress=80
npx wrangler deploy

live --data-urlencode live=end --data-urlencode "status=部署完成"
```

## 其他会出现卡片的地方 [#其他会出现卡片的地方]

- **服务集成**：GitHub Actions、GitLab pipeline、Vercel 和 Cloudflare 的部署运行中，可以自动显示卡片。在那个集成的设置里打开即可。
- **AI 代理**：MCP 的 `notify` 工具也接受 `live`、`status`、`progress`、`state`、`alert`，代理可以用同样的方式显示进度。
