diff --git a/README-notifications.md b/README-notifications.md new file mode 100644 index 0000000..6950b16 --- /dev/null +++ b/README-notifications.md @@ -0,0 +1,375 @@ +# 推送通知功能使用指南 + +## 快速开始 + +### 1. 安装依赖 + +推送通知功能已经集成到项目中,无需额外安装依赖。 + +### 2. 基本使用 + +在组件中使用推送通知功能: + +```typescript +import { useNotifications } from '@/hooks/useNotifications'; + +function MyComponent() { + const { sendNotification, sendWorkoutReminder } = useNotifications(); + + const handleSendNotification = async () => { + await sendNotification({ + title: '测试通知', + body: '这是一个测试通知', + sound: true, + priority: 'high' + }); + }; + + const handleSendWorkoutReminder = async () => { + await sendWorkoutReminder('运动提醒', '该开始今天的普拉提训练了!'); + }; + + return ( + // 你的组件内容 + ); +} +``` + +### 3. 使用辅助函数 + +```typescript +import { WorkoutNotificationHelpers, GoalNotificationHelpers } from '@/utils/notificationHelpers'; + +// 发送运动开始提醒 +await WorkoutNotificationHelpers.sendWorkoutStartReminder('张三'); + +// 发送目标达成通知 +await GoalNotificationHelpers.sendGoalAchievementNotification('张三', '每周运动3次'); + +// 安排每日运动提醒 +await WorkoutNotificationHelpers.scheduleDailyWorkoutReminder('张三', 9, 0); // 每天上午9点 +``` + +## 功能特性 + +### ✅ 已实现功能 + +- [x] 立即发送通知 +- [x] 定时发送通知 +- [x] 重复发送通知 +- [x] 通知权限管理 +- [x] 通知点击处理 +- [x] 通知取消功能 +- [x] 多种通知类型支持 +- [x] 通知模板系统 +- [x] 批量通知发送 +- [x] 通知状态查询 + +### 📱 支持的通知类型 + +1. **运动相关** + - 运动开始提醒 + - 运动完成通知 + - 每日运动提醒 + +2. **目标相关** + - 目标达成通知 + - 目标进度更新 + - 目标截止提醒 + +3. **心情相关** + - 心情打卡提醒 + - 每日心情提醒 + +4. **营养相关** + - 营养记录提醒 + - 三餐提醒 + +5. **通用通知** + - 欢迎通知 + - 成就通知 + - 系统维护通知 + +## 详细使用示例 + +### 1. 在运动页面中使用 + +```typescript +import { WorkoutNotificationHelpers } from '@/utils/notificationHelpers'; + +// 运动开始时 +const handleWorkoutStart = async () => { + await WorkoutNotificationHelpers.sendWorkoutStartReminder(userName); + // 开始运动逻辑 +}; + +// 运动完成时 +const handleWorkoutComplete = async (duration: number) => { + await WorkoutNotificationHelpers.sendWorkoutCompleteNotification(userName, duration); + // 完成运动逻辑 +}; +``` + +### 2. 在目标页面中使用 + +```typescript +import { GoalNotificationHelpers } from '@/utils/notificationHelpers'; + +// 目标达成时 +const handleGoalAchieved = async (goalName: string) => { + await GoalNotificationHelpers.sendGoalAchievementNotification(userName, goalName); + // 目标达成逻辑 +}; + +// 目标进度更新时 +const handleGoalProgress = async (goalName: string, progress: number) => { + if (progress >= 50 && progress < 100) { + await GoalNotificationHelpers.sendGoalProgressNotification(userName, goalName, progress); + } +}; +``` + +### 3. 在心情页面中使用 + +```typescript +import { MoodNotificationHelpers } from '@/utils/notificationHelpers'; + +// 安排每日心情打卡提醒 +const setupMoodReminder = async () => { + await MoodNotificationHelpers.scheduleDailyMoodReminder(userName, 20, 0); // 每天晚上8点 +}; +``` + +### 4. 在营养页面中使用 + +```typescript +import { NutritionNotificationHelpers } from '@/utils/notificationHelpers'; + +// 安排营养记录提醒 +const setupNutritionReminders = async () => { + await NutritionNotificationHelpers.scheduleNutritionReminders(userName); +}; +``` + +## 通知模板使用 + +```typescript +import { NotificationTemplates } from '@/utils/notificationHelpers'; +import { notificationService } from '@/services/notifications'; + +// 使用模板发送通知 +const sendWorkoutNotification = async (userName: string) => { + const notification = NotificationTemplates.workout.start(userName); + await notificationService.sendImmediateNotification(notification); +}; + +const sendGoalNotification = async (userName: string, goalName: string) => { + const notification = NotificationTemplates.goal.achievement(userName, goalName); + await notificationService.sendImmediateNotification(notification); +}; +``` + +## 权限管理 + +### 检查权限状态 + +```typescript +import { useNotifications } from '@/hooks/useNotifications'; + +function MyComponent() { + const { permissionStatus, requestPermission } = useNotifications(); + + const handleRequestPermission = async () => { + const status = await requestPermission(); + if (status === 'granted') { + console.log('通知权限已授予'); + } else { + console.log('通知权限被拒绝'); + } + }; + + return ( + + 权限状态: {permissionStatus} +