|
|
本帖最后由 Millionaire 于 2026-2-23 00:07 编辑
ntfy是开源的消息通知服务,你可以选择自托管ntfy服务器,以实现自定义的管理控制,然后把它用于发送和接收重要的消息通知。
一、安装ntfy
- sudo rpm -ivh https://github.com/binwiederhier/ntfy/releases/download/v2.16.0/ntfy_2.16.0_linux_amd64.rpm
- sudo systemctl enable ntfy
- sudo systemctl start ntfy
复制代码
二、配置
打开server.yml文件:
- sudo vi /etc/ntfy/server.yml
复制代码 配置base-url、listen-http、attachment-cache-dir和web-root,并修改base-url的域名:
- base-url: "https://ntfy.example.com"
- listen-http: "127.0.0.1:2586"
- attachment-cache-dir: "/var/cache/ntfy/attachments"
- web-root: disable
复制代码
三、启动ntfy
启动ntfy服务:
如果没有使用“web-root:disable”参数关闭Web应用,可以打开http://127.0.0.1:2586访问ntfy的Web应用。
四、反向代理
添加ntfy.example.com的DNS记录,并配置Apache反向代理:- <VirtualHost *:443>
- ServerName ntfy.example.com
- ProxyPass "/" "http://127.0.0.1:2586/"
- ProxyPassReverse "/" "http://127.0.0.1:2586/"
- SSLCertificateFile /etc/letsencrypt/live/ntfy.example.com/fullchain.pem
- SSLCertificateKeyFile /etc/letsencrypt/live/ntfy.example.com/privkey.pem
- Include /etc/letsencrypt/options-ssl-apache.conf
- </VirtualHost>
复制代码
五、发送消息
在手机接收通知需要下载并安装ntfy,然后打开应用订阅主题。
订阅后,通过命令向“ntfy.example.com/YuALPKCpAgiE2pg”主题地址发送消息:
- ntfy publish ntfy.example.com/YuALPKCpAgiE2Epg "OK"
复制代码 curl方式:
- curl -d "Ok" ntfy.example.com/YuALPKCpAgiE2Epg
复制代码 HTTP方式:
- POST /YuALPKCpAgiE2Epg HTTP/1.1
- Host: ntfy.example.com
- OK
复制代码 JavaScript:
- fetch('https://ntfy.example.com/YuALPKCpAgiE2Epg', {
- method: 'POST', // PUT也有效
- body: 'OK'
- })
复制代码 Go:
- http.Post("https://ntfy.example.com/YuALPKCpAgiE2Epg", "text/plain", strings.NewReader("OK"))
复制代码 Python:
- requests.post("https://ntfy.example.com/YuALPKCpAgiE2Epg", data="OK".encode(encoding='utf-8'))
复制代码 PHP:
- file_get_contents('https://ntfy.example.com/YuALPKCpAgiE2Epg', false, stream_context_create([
- 'http' =>
- 'method' => 'POST', // PUT 也有效
- 'header' => 'Content-Type: text/plain',
- 'content' => 'OK'
- ]
- ]));
复制代码 此时,在手机上就会收到刚才发送的消息通知。为了让ntfy保持在后台运行并提升通知效果,建议对客户端进行以下设置:
- 忽略电池优化
- 允许自启动、允许关联启动、允许后台活动
- 关闭ntfy的“订阅服务”通知
- 设置通知铃声
六、访问控制
ntfy服务器默认对所有人开放,任何人都可以读写主题。若要限制他人访问,则需要进行认证配置:
- auth-file: "/var/lib/ntfy/user.db"
- auth-default-access: "deny-all"
- auth-users:
- - "admin:$2a$10$nYeIjwXLyvb/p4jlYKMLWOZvrUthvfPkGB8HgJo3GP1hwwwsh/tuO:admin"
- - "ntfy:$2a$10$nya0qNsGWo2Y3RCPW8GNfeVNU6ORcA5wBRUCFRwqasaxwQzjvMzx6:user"
- auth-access:
- - "ntfy:*:rw"
- auth-tokens:
- - "ntfy:tk_5hhwe5wxp9lk34e5qjkre9hn62gz7"
复制代码 |
|