本文目录导读:

Python 方案(跨平台)
安装依赖
pip install plyer schedule
简单定时通知脚本
import schedule
import time
from plyer import notification
def send_notification(title, message):
"""发送桌面通知"""
notification.notify(
title=title,
message=message,
timeout=10, # 显示时间(秒)
app_icon=None # 可选的图标路径
)
def morning_task():
send_notification("早安提醒", "新的一天开始了,加油!")
def hourly_reminder():
send_notification("定时提醒", f"现在是 {time.strftime('%H:%M')},该休息一下了")
# 设置定时任务
schedule.every().day.at("09:00").do(morning_task)
schedule.every().hour.do(hourly_reminder)
schedule.every().day.at("12:00").do(lambda: send_notification("午餐时间", "记得吃饭哦!"))
schedule.every().day.at("17:00").do(lambda: send_notification("下班提醒", "今天辛苦了,早点休息"))
# 5分钟提醒一次
schedule.every(5).minutes.do(lambda: send_notification("定时提醒", "活动一下身体"))
# 运行循环
while True:
schedule.run_pending()
time.sleep(1)
Windows 专用批处理 + VBScript
创建 .bat 文件
@echo off :loop echo 发送通知... powershell -Command "New-BurntToastNotification -Text '定时提醒', '该休息一下了!'" timeout /t 3600 /nobreak REM 3600秒 = 1小时 goto loop
使用 Windows 原生命令
@echo off
REM 使用弹窗通知
msg * "定时提醒:休息时间到了!"
REM 或者使用PowerShell
powershell -Command "[System.Windows.Forms.MessageBox]::Show('休息时间到了','定时提醒')"
macOS 方案(AppleScript)
-- AppleScript 发送通知 display notification "该休息一下了" with title "定时提醒" -- 配合 .sh 脚本定时执行
shell 脚本版本
#!/bin/bash osascript -e 'display notification "休息时间到了" with title "定时提醒"' # 使用 launchd 定时执行
Node.js 方案
安装依赖
npm install node-notifier node-schedule
const notifier = require('node-notifier');
const schedule = require('node-schedule');
// 发送通知函数
function sendNotification(title, message) {
notifier.notify({
title: title,
message: message,
timeout: 10,
sound: true,
wait: true
});
}
// 定时任务
// 每天早上9点
schedule.scheduleJob('0 9 * * *', () => {
sendNotification('早安', '新的一天开始了');
});
// 每小时提醒
schedule.scheduleJob('0 * * * *', () => {
sendNotification('定时提醒', '请注意休息');
});
// 自定义时间(周一到周五下午5点)
schedule.scheduleJob('0 17 * * 1-5', () => {
sendNotification('下班时间', '今天辛苦了!');
});
console.log('定时通知脚本已启动');
高级用法 - 可配置脚本
import schedule
import time
import json
import os
from plyer import notification
from datetime import datetime
class NotificationScheduler:
def __init__(self, config_file='notifications.json'):
self.config_file = config_file
self.notifications = []
self.load_config()
def load_config(self):
"""加载配置文件"""
if os.path.exists(self.config_file):
with open(self.config_file, 'r', encoding='utf-8') as f:
self.notifications = json.load(f)
def add_notification(self, title, message, time, day='*'):
"""添加通知"""
self.notifications.append({
'title': title,
'message': message,
'time': time,
'day': day
})
self.save_config()
def save_config(self):
"""保存配置"""
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(self.notifications, f, ensure_ascii=False, indent=2)
def start(self):
"""启动调度器"""
for notification in self.notifications:
self._schedule_notification(notification)
while True:
schedule.run_pending()
time.sleep(1)
def _schedule_notification(self, config):
"""安排单个通知"""
title = config['title']
message = config['message']
schedule_time = config['time']
day = config.get('day', '*')
# 根据星期几安排
if day == 'mon-fri':
schedule.every().monday.at(schedule_time).do(
self._send, title, message)
schedule.every().tuesday.at(schedule_time).do(
self._send, title, message)
schedule.every().wednesday.at(schedule_time).do(
self._send, title, message)
schedule.every().thursday.at(schedule_time).do(
self._send, title, message)
schedule.every().friday.at(schedule_time).do(
self._send, title, message)
else:
getattr(schedule.every(), day).at(schedule_time).do(
self._send, title, message)
def _send(self, title, message):
"""发送通知"""
notification.notify(
title=title,
message=message,
timeout=10,
app_name="定时提醒"
)
print(f"[{datetime.now()}] 发送通知: {title}")
# 使用示例
if __name__ == "__main__":
scheduler = NotificationScheduler()
# 添加自定义通知
scheduler.add_notification("晨会提醒", "上午9点晨会开始", "09:00")
scheduler.add_notification("喝水提醒", "该喝水了,保持健康!", "10:30", "mon-fri")
# 启动调度器
scheduler.start()
常用时间表达式
| 场景 | 表达式 | 说明 |
|---|---|---|
| 每小时 | schedule.every().hour |
每小时执行 |
| 每天特定时间 | schedule.every().day.at("14:30") |
每天下午2:30 |
| 每周一 | schedule.every().monday.at("09:00") |
每星期一上午9点 |
| 每30分钟 | schedule.every(30).minutes |
每30分钟 |
| 工作日 | schedule.every().monday到friday |
仅工作日 |
注意事项
- 权限:某些系统可能需要在通知设置中允许应用发送通知
- 资源占用:脚本需要保持运行才能持续定时发通知
- 系统休眠:电脑休眠时定时任务可能不会执行
- 时区:注意服务器或电脑所在时区
选择适合你需求的方案,根据需要调整发送时间和内容即可。