ntfy(发音为 notify)是一个简单的基于 HTTP 的 pub-sub 通知服务。它允许您通过任何计算机的脚本和/或使用 REST API 将通知发送到您的手机或桌面。它是无限灵活的,并且是 100% 的免费软件。
获取应用
要在手机上接收通知,请通过 Google Play、App Store 或 F-Droid 安装该应用。安装后,打开它并订阅您选择的主题。无需显式创建主题,因此只需选择一个名称,并在以后发布消息时使用即可。请注意,主题名称是公开的,因此选择不容易猜到的内容是明智的。
可以在 ntfy.sh 处访问 ntfy 的免费版本。Google Play 或 F-Droid 上还有一个开源 Android 应用,App Store 上还有一个开源 iOS 应用。
创建/添加您的第一个主题
点击“订阅”后,该应用程序正在收听有关该主题的新消息。
发送消息
现在,让我们向我们的主题发送一条消息。这在每种语言中都很容易,因为我们只使用 HTTP PUT/POST 或 ntfy CLI。消息位于请求正文中。以下示例显示了如何使用 POST 请求发布简单消息:
-
Curl
curl -d "Backup successful 😀" ntfy.sh/mytopic
-
ntfy CLI
ntfy publish mytopic "Backup successful 😀"
-
HTTP
POST /mytopic HTTP/1.1 Host: ntfy.sh Backup successful 😀
-
JavaScript
fetch('https://ntfy.sh/mytopic', { method: 'POST', // PUT works too body: 'Backup successful 😀' })
-
Go
http.Post("https://ntfy.sh/mytopic", "text/plain", strings.NewReader("Backup successful 😀"))
-
Python
requests.post("https://ntfy.sh/mytopic", data="Backup successful 😀".encode(encoding='utf-8'))
-
PHP
file_get_contents('https://ntfy.sh/mytopic', false, stream_context_create([ 'http' => [ 'method' => 'POST', // PUT also works 'header' => 'Content-Type: text/plain', 'content' => 'Backup successful 😀' ] ]));
这将创建一个通知,如下所示: