Ubuntu消息推送设置步骤
导读:Ubuntu 消息推送设置步骤 一 系统桌面通知启用与关闭 打开系统设置:点击Activities搜索并进入Settings,在侧栏选择Notifications。 全局开关:在顶部将Notifications设为On/Off,统一管理是...
Ubuntu 消息推送设置步骤
一 系统桌面通知启用与关闭
- 打开系统设置:点击Activities搜索并进入Settings,在侧栏选择Notifications。
- 全局开关:在顶部将Notifications设为On/Off,统一管理是否显示通知。
- 按应用设置:在列表中点击具体应用,按需开启或关闭Sound、Alerts等选项,或完全关闭某个应用的通知。
- 命令行快速关闭系统升级提示(GNOME 系):打开Software & Updates → Updates,将Notify me of a new Ubuntu version设为Never。
二 定时桌面通知 notify send 与 crontab
- 安装通知工具(如未安装):sudo apt-get install libnotify-bin
- 发送测试通知:notify-send “标题” “内容” -i /path/icon.png -t 1000
- 使用 crontab 定时推送(示例:工作日 10:00 提醒)
- 编辑任务:crontab -e
- 增加条目(注意会话环境变量与显示变量):
* 10 * * 1-5 . /home/yourname/notify_dinner - 在脚本 notify_dinner 中设置环境变量并发送通知:
#!/usr/bin/env bash eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)" export DISPLAY=:0 notify-send "Lunch Reminder" "Time to submit OA." - 说明:crontab 时间格式为分 时 日 月 周 命令;如任务无输出可重定向至**/dev/null 2> & 1**以减少日志。
三 本机 MQTT 消息推送 Mosquitto
- 安装与启动:
- 安装:sudo apt-get install mosquitto mosquitto-clients
- 后台运行:sudo mosquitto -d
- 订阅与发布(本机测试):
- 订阅:mosquitto_sub -h 127.0.0.1 -t “demo” -v
- 发布:mosquitto_pub -h 127.0.0.1 -t “demo” -m “Hello MQTT”
- 带认证与权限(示例用户 test,密码 123)
- 生成密码文件:sudo mosquitto_passwd /etc/mosquitto/pwfile test
- 配置 /etc/mosquitto/mosquitto.conf:
password_file /etc/mosquitto/pwfile acl_file /etc/mosquitto/aclfile - 配置 ACL(/etc/mosquitto/aclfile):
user test topic read demo topic write demo - 重启生效:sudo mosquitto -c /etc/mosquitto/mosquitto.conf -d
- 带账号测试:
- 订阅:mosquitto_sub -h 192.168.1.1 -t “demo” -u test -P 123 -v
- 发布:mosquitto_pub -h 192.168.1.1 -t “demo” -u test -P 123 -m “Hello”
四 Web 实时推送 WebSocket 示例 JSP Tomcat
- 安装运行环境:sudo apt update & & sudo apt install openjdk-11-jdk;部署 Apache Tomcat 9(解压至/opt/tomcat 并启动)。
- 服务端端点(Java WebSocket,使用 @ServerEndpoint 注解,示例路径 /message):
- 维护会话集合,处理 @OnOpen/@OnClose/@OnMessage,遍历向已连接会话 sendText。
- 前端页面(index.jsp):
- 建立连接:new WebSocket(“ws://localhost:8080/your-app-context/message”)
- 监听 onmessage 将收到的文本追加到页面消息区;提供输入框与发送按钮调用 ws.send()。
- 部署与验证:将应用打包为 WAR 放入 Tomcat webapps,启动后在浏览器访问对应上下文测试收发。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu消息推送设置步骤
本文地址: https://pptw.com/jishu/778173.html
