模拟第三方信息存入数据库并进行消息推送

This commit is contained in:
not-like-juvenile
2026-03-09 17:27:56 +08:00
parent 436b7b251f
commit ee9fabd806
5 changed files with 413 additions and 29 deletions

View File

@@ -124,6 +124,35 @@ UNIPUSH 集成注意事项
- 如果 `/rest/v1/push_devices` 返回 404确认表名在 `public` schema 中并加载,或调整请求前缀。
- 查看 push-server 控制台输出中的 `supaFetch` warn 和 proxy 响应体以获取具体错误信息。
- 如果 `/api/v1/push/send` 返回 HTTP 200 但 body 为 `{ errCode: 400, errMsg: "push_clientid required" }`
- 含义:云函数没有拿到 `push_clientid`,因此没有真正执行 uni-push 下发。
- 常见原因:`CLOUD_FUNC_URL` 指向 HTTP 触发云函数cloudbasefunction.cn 等),请求体通常在 `event.body`(字符串)中。
- 云函数侧最小兼容写法:先把 `event.body` 解析为 JSON再从解析结果解构 `push_clientid/title/content`
- 如果手机通知标题/内容变成 `????`
- 含义中文在发出请求前已被错误编码Windows PowerShell 5.1 常见)。
- 解决:用 UTF-8 字节发送 JSON并设置 `application/json; charset=utf-8`)。示例:
```powershell
$bodyObj = @{
cids = @('YOUR_DEVICE_CID')
notification = @{ title = '测试标题'; body = '测试内容' }
payload = @{ order_id = '123' }
}
$json = $bodyObj | ConvertTo-Json -Depth 10
$bytes = [System.Text.Encoding]::UTF8.GetBytes($json)
Invoke-RestMethod -Method Post -Uri 'http://127.0.0.1:7301/api/v1/push/send' `
-ContentType 'application/json; charset=utf-8' `
-Body $bytes | ConvertTo-Json -Depth 20
```
- 备注:也可用 `curl.exe` 发送 JSON避免 PowerShell 的编码差异。
- 如果你在云函数里加了“回显 recv 并提前 return”用于排查乱码
- 排查完成后务必移除该提前 return否则云函数不会执行 `uniPush.sendMessage()`,手机将收不到通知。
后续建议(可选实现)
-`express_notifications` 增加 `attempts``error``sent_at` 字段以支持重试与错误记录;可实现后台 worker 或 pg_notify+listener 做可靠投递与重试。
-`/api/v1/push/send``/api/v1/notifications` 添加管理员鉴权(例如 `PUSH_ADMIN_KEY`)以限制谁能发送通知。