在手机上安装好客户端,订阅服务器消息,任何人将可以发送消息到您的手机上。服务器可以自建,也可以用官网,使用简单。
- 主页:https://ntfy.sh
- GitHub(服务端):https://github.com/binwiederhier/ntfy
- GitHub(安卓端):https://github.com/binwiederhier/ntfy-android
- 文档:https://docs.ntfy.sh/publish/
当然,它也有苹果客户端。可能苹果消息机制的原因,我没有收到信息。
服务端居然是用Golang写的。
发送消息: curl -d “Your backup is done” ntfy.sh/mytopic
这里的mytopic就是手机上设置的主题名称
比如我订单了ease主题在手机端,您可以发送: curl -d “你好,兄弟” ntfy.sh/ease 我将实时的接收到消息。
想想有什么用呢?比如互订消息用来聊天?当然一些通知信息很简单(之前都是通过微信接收)
基于IP地址的帐户消息限制每天500条,应该够用了。
视频演示:https://docs.ntfy.sh/static/img/android-video-overview.mp4
几种发送方式:
命令行:curl -d “Backup successful 😀” ntfy.sh/mytopic
JavaScript:
fetch('https://ntfy.sh/mytopic', {
method: 'POST', // PUT works too
body: 'Backup successful 😀'
})
Golang: http.Post(“https://ntfy.sh/mytopic", “text/plain”, strings.NewReader(“Backup successful 😀”)) …
更复杂一点,主要是标题,图标等。看起来是在消息头上做的文章:
req, _ := http.NewRequest("POST", "https://ntfy.sh/phil_alerts",
strings.NewReader("Remote access to phils-laptop detected. Act right away."))
req.Header.Set("Title", "Unauthorized access detected")
req.Header.Set("Priority", "urgent")
req.Header.Set("Tags", "warning,skull")
http.DefaultClient.Do(req)
在PowerShell中使用:
$Request = @{
Method = "POST"
URI = "https://ntfy.sh/phil_alerts"
Headers = @{
Title = "Unauthorized access detected"
Priority = "urgent"
Tags = "warning,skull"
}
Body = "Remote access to phils-laptop detected. Act right away."
}
Invoke-RestMethod @Request
在命令行中使用:
curl \
-H "Title: Unauthorized access detected" \
-H "Priority: urgent" \
-H "Tags: warning,skull" \
-d "Remote access to phils-laptop detected. Act right away." \
ntfy.sh/phil_alerts
看起来还可以发送链接和图片
curl \
-H "Click: https://home.nest.com/" \
-H "Attach: https://nest.com/view/yAxkasd.jpg" \
-H "Actions: http, Open door, https://api.nest.com/open/yAxkasd, clear=true" \
-H "Email: phil@example.com" \
-d "There's someone at the door. 🐶
Please check if it's a good boy or a hooman.
Doggies have been known to ring the doorbell." \
ntfy.sh/mydoorbell
还有预订消息(定时提醒),可以设置10秒至3天内。
curl -H “At: tomorrow, 10am” -d “Good morning” ntfy.sh/hello
应用场景:
- SSH登陆警报: nano /etc/pam.d/sshd
# at the end of the file
session optional pam_exec.so /usr/bin/ntfy-ssh-login.sh
nano /usr/bin/ntfy-ssh-login.sh
#!/bin/bash
if [ "${PAM_TYPE}" = "open_session" ]; then
curl \
-H prio:high \
-H tags:warning \
-d "SSH login: ${PAM_USER} from ${PAM_RHOST}" \
ntfy.sh/alerts
fi
用golang来作一个消息监听
package main
import (
"bufio"
"log"
"net/http"
)
func main() {
resp, err := http.Get("https://ntfy.sh/ease/json")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
println(scanner.Text())
}
}
返回信息:
{"id":"jsPi3Cf4m99s","time":1684723464,"event":"open","topic":"ease"}
{"id":"YX84NAtX3N29","time":1684723509,"event":"keepalive","topic":"ease"}
{"id":"gaXfj5IOS5TT","time":1684723520,"expires":1684766720,"event":"message","topic":"ease","title":"测试的主题","message":"测试的内容","priority":4,"tags":["grin"]}
{"id":"prIy5Jrwxgxu","time":1684723554,"event":"keepalive","topic":"ease"}
{"id":"QCWWznwNzvsu","time":1684723599,"event":"keepalive","topic":"ease"}
通过轮循的方式获取信息: ntfy.sh/mytopic/json?poll=1
提取缓存的消息:
curl -s “ntfy.sh/mytopic/json?since=10m”
curl -s “ntfy.sh/mytopic/json?since=1645970742”
curl -s “ntfy.sh/mytopic/json?since=nFS3knfcQ1xe”
2023.5.22
与Autohotkey结合,通过发送消息来关闭或重启我的电脑。
2023.6.10
https://gotify.net/ 一个有点类似的项目
2023.6.11
在服务器搬迁后,写入定时任务/etc/crontb中,让它每天23点报告服务器情况:0 23 * * * * root /home/ease/tools/ease-sysinfo | curl -X POST –data-binary @- https://ntfy.sh/ease
服务器相关输出在ease-sysinfo脚本中