找回密码
 立即注册
查看: 386|回复: 0

ntfy消息通知服务的使用

[复制链接]

69

主题

0

回帖

11万

积分

VIP

积分
117731
发表于 2026-2-2 21:14:54 | 显示全部楼层 |阅读模式
本帖最后由 Millionaire 于 2026-2-23 00:07 编辑

img-1.jpg

ntfy是开源的消息通知服务,你可以选择自托管ntfy服务器,以实现自定义的管理控制,然后把它用于发送和接收重要的消息通知。

一、安装ntfy
  1. sudo rpm -ivh https://github.com/binwiederhier/ntfy/releases/download/v2.16.0/ntfy_2.16.0_linux_amd64.rpm
  2. sudo systemctl enable ntfy
  3. sudo systemctl start ntfy
复制代码

二、配置
打开server.yml文件:
  1. sudo vi /etc/ntfy/server.yml
复制代码
配置base-url、listen-http、attachment-cache-dir和web-root,并修改base-url的域名:
  1. base-url: "https://ntfy.example.com"
  2. listen-http: "127.0.0.1:2586"
  3. attachment-cache-dir: "/var/cache/ntfy/attachments"
  4. web-root: disable
复制代码

三、启动ntfy
启动ntfy服务:
  1. sudo ntfy serve
复制代码
如果没有使用“web-root:disable”参数关闭Web应用,可以打开http://127.0.0.1:2586访问ntfy的Web应用。

四、反向代理
添加ntfy.example.com的DNS记录,并配置Apache反向代理:
  1. <VirtualHost *:443>
  2.     ServerName ntfy.example.com

  3.     ProxyPass "/" "http://127.0.0.1:2586/"
  4.     ProxyPassReverse "/" "http://127.0.0.1:2586/"

  5. SSLCertificateFile /etc/letsencrypt/live/ntfy.example.com/fullchain.pem
  6. SSLCertificateKeyFile /etc/letsencrypt/live/ntfy.example.com/privkey.pem
  7. Include /etc/letsencrypt/options-ssl-apache.conf
  8. </VirtualHost>
复制代码

五、发送消息
在手机接收通知需要下载并安装ntfy,然后打开应用订阅主题。

img-2.jpg

订阅后,通过命令向“ntfy.example.com/YuALPKCpAgiE2pg”主题地址发送消息:
  1. ntfy publish ntfy.example.com/YuALPKCpAgiE2Epg "OK"
复制代码
curl方式:
  1. curl -d "Ok" ntfy.example.com/YuALPKCpAgiE2Epg
复制代码
HTTP方式:
  1. POST /YuALPKCpAgiE2Epg HTTP/1.1
  2. Host: ntfy.example.com

  3. OK
复制代码
JavaScript:
  1. fetch('https://ntfy.example.com/YuALPKCpAgiE2Epg', {
  2.     method: 'POST', // PUT也有效
  3.     body: 'OK'
  4. })
复制代码
Go:
  1. http.Post("https://ntfy.example.com/YuALPKCpAgiE2Epg", "text/plain", strings.NewReader("OK"))
复制代码
Python:
  1. requests.post("https://ntfy.example.com/YuALPKCpAgiE2Epg", data="OK".encode(encoding='utf-8'))
复制代码
PHP:
  1. file_get_contents('https://ntfy.example.com/YuALPKCpAgiE2Epg', false, stream_context_create([
  2.     'http' =>
  3.         'method' => 'POST', // PUT 也有效
  4.         'header' => 'Content-Type: text/plain',
  5.         'content' => 'OK'
  6.     ]
  7. ]));
复制代码
此时,在手机上就会收到刚才发送的消息通知。为了让ntfy保持在后台运行并提升通知效果,建议对客户端进行以下设置:
  • 忽略电池优化
  • 允许自启动、允许关联启动、允许后台活动
  • 关闭ntfy的“订阅服务”通知
  • 设置通知铃声

img-3.jpg

六、访问控制
ntfy服务器默认对所有人开放,任何人都可以读写主题。若要限制他人访问,则需要进行认证配置:
  1. auth-file: "/var/lib/ntfy/user.db"
  2. auth-default-access: "deny-all"
  3. auth-users:
  4.   - "admin:$2a$10$nYeIjwXLyvb/p4jlYKMLWOZvrUthvfPkGB8HgJo3GP1hwwwsh/tuO:admin"
  5.   - "ntfy:$2a$10$nya0qNsGWo2Y3RCPW8GNfeVNU6ORcA5wBRUCFRwqasaxwQzjvMzx6:user"
  6. auth-access:
  7.   - "ntfy:*:rw"
  8. auth-tokens:
  9.   - "ntfy:tk_5hhwe5wxp9lk34e5qjkre9hn62gz7"
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

VIP会员|Archiver|小黑屋|手机版|i am Bug

GMT+8, 2026-6-5 09:11 , Processed in 0.017757 second(s), 22 queries .

Powered by Discuz!

Copyright © 2021-2026, i am Bug.

快速回复 返回顶部 返回列表