NotIf 文档 价格 应用

通过 API 订阅

你的程序可以通过 HTTP 或 WebSocket 接收频道的消息。要发布消息,请参见发布。

JSON 流

curl -s https://usenotif.com/mes-sauvegardes/json

连接会保持打开,每个事件以一行 JSON 送达:

{"id":"cGBLzzR9VgJo","time":1793541469,"event":"open","topic":"mes-sauvegardes"}
{"id":"IKdMz9CGFTiC","time":1793541470,"event":"message","topic":"mes-sauvegardes","message":"复制完成","expires":1793584670}

每 30 秒会收到一个 keepalive 事件,以保持连接。

SSE 流

Server-Sent Events 格式适用于浏览器中的 EventSource。

curl -s https://usenotif.com/mes-sauvegardes/sse
event: open
data: {"id":"m2wCK8blvUwG","time":1793541473,"event":"open","topic":"mes-sauvegardes"}

id: TgDx1jhsK5LH
data: {"id":"TgDx1jhsK5LH","time":1793541474,"event":"message","topic":"mes-sauvegardes","message":"复制完成","expires":1793584674}

message、message_delete 和 message_clear 都没有 event: 行。请读取 JSON 中的 event 字段来区分它们。open 和 keepalive 带有 event: 行。

文本流

/raw 只返回文字,每行一条消息。消息中的换行会被替换为空格。keepalive 会产生一个空行。

curl -s https://usenotif.com/mes-sauvegardes/raw

WebSocket

连接到 wss://usenotif.com/mes-sauvegardes/ws。每条 WebSocket 消息包含一个 JSON 事件,格式与 /json 相同。过滤器、since 和 auth 的用法也相同。

const ws = new WebSocket('wss://usenotif.com/mes-sauvegardes/ws?since=1h');
ws.onmessage = e => console.log(JSON.parse(e.data));

多个频道

用逗号分隔频道,每个连接最多 150 个。

curl -s https://usenotif.com/mes-sauvegardes,ma-maison/json

你必须对所有这些频道都有读取权限。只要有一个被拒绝,整个请求就会失败。

读取后不保持连接

poll=1 会返回保存的消息,然后关闭连接。

curl -s "https://usenotif.com/mes-sauvegardes/json?poll=1"

消息保留 12 小时。如果每个频道的响应超过 10 MB,最早的消息会被省略,响应中会包含 X-Messages-Truncated: 1 请求头。

从指定位置继续

since 用于选择起点。没有 since 时,持续连接只返回新消息,poll=1 则返回所有保存的消息。

curl -s "https://usenotif.com/mes-sauvegardes/json?poll=1&since=1h"
值 效果
all 所有保存的消息
latest 仅最后一条消息
none 不返回旧消息
10m, 1h, 2 days 从这段时间之前开始
1793541403 从这个 Unix 时间戳开始
8sPiEtwk83R2 在这条消息(ID)之后

为了在断线后不遗漏任何消息,请保存最后一条消息的 id,并在重新连接时把它传给 since。

加上 scheduled=1 可以包含尚未送达的定时消息。

curl -s "https://usenotif.com/mes-rappels/json?poll=1&scheduled=1"

过滤消息

参数 别名 效果
id 完全匹配的 ID
message m 完全匹配的文字,不区分大小写
title t 完全匹配的标题,不区分大小写
priority prio, p 以逗号分隔的优先级,匹配其中任一
tags tag, ta 以逗号分隔的标签,须全部匹配
curl -s "https://usenotif.com/mes-sauvegardes/json?poll=1&priority=high,urgent"
curl -s "https://usenotif.com/mes-sauvegardes/json?tags=warning,disque"

没有优先级的消息按优先级 3 计算。过滤器只作用于 message 事件。

所有这些参数也可以用作请求头:X-Poll、X-Since、X-Scheduled、X-Priority 等。po、si 和 sched 是 poll、since 和 scheduled 的别名。

受保护的频道

像发布时一样传递你的令牌。请参见账户。

curl -s -H "Authorization: Bearer tk_votrejeton" https://usenotif.com/mon-canal-prive/json

在浏览器中使用 EventSource 或 WebSocket 时,它们不接受请求头,请使用 auth 参数。

在不读取消息的情况下检查读取权限:

curl -H "Authorization: Bearer tk_votrejeton" https://usenotif.com/mon-canal-prive/auth

消息格式

{
  "id": "OwR5sRK7eWPN",
  "time": 1793541433,
  "expires": 1793584633,
  "event": "message",
  "topic": "mes-sauvegardes",
  "title": "备份完成",
  "message": "已复制 12 GB",
  "priority": 4,
  "tags": ["floppy_disk"],
  "click": "https://usenotif.com/docs",
  "icon": "https://usenotif.com/icons/icon-192.png",
  "actions": [{"action": "view", "label": "详情", "url": "https://usenotif.com/docs", "clear": false}],
  "attachment": {
    "name": "rapport.pdf",
    "type": "application/pdf",
    "size": 52041,
    "expires": 1793552233,
    "url": "https://usenotif.com/file/E26jUBV5Cm85.pdf"
  },
  "content_type": "text/markdown",
  "sequence_id": "copie-42"
}

只有 id、time、event 和 topic 总是存在。其他字段在有值时才会出现。

字段 内容
id 12 个字符的 ID
time 送达时的 Unix 时间戳
expires 从服务器删除时的 Unix 时间戳
event 事件类型
topic 频道。在 open 中为频道列表
message 文字
title 标题
priority 1 到 5。缺省时:优先级 3
tags 标签列表
click 点击链接
icon 图标地址
actions 按钮列表
attachment 附件。对于在线文件,只有 name 和 url
content_type 消息为 Markdown 时为 text/markdown
sequence_id 序列 ID
encoding 某些 UnifiedPush 消息为 base64

事件类型

事件 含义
open 连接已建立
keepalive 保持连接
message 新消息
message_clear 该序列的消息已读:请关闭它的通知
message_delete 该序列的消息已删除
poll_request 客户端应重新读取该频道

处理更新时,请保留每个 sequence_id 的最后一条消息。被替换和被删除的消息会一直保留到过期,并会在使用 since 继续读取时再次出现。