Files
digital-pilates/docs/notification-reminders-implementation.md
richarjiang a34ca556e8 feat(notifications): 新增晚餐和心情提醒功能,支持HRV压力检测和后台处理
- 新增晚餐提醒(18:00)和心情提醒(21:00)的定时通知
- 实现基于HRV数据的压力检测和智能鼓励通知
- 添加后台任务处理支持,修改iOS后台模式为processing
- 优化营养记录页面使用Redux状态管理,支持实时数据更新
- 重构卡路里计算公式,移除目标卡路里概念,改为基代+运动-饮食
- 新增营养目标动态计算功能,基于用户身体数据智能推荐
- 完善通知点击跳转逻辑,支持多种提醒类型的路由处理
2025-09-01 10:29:13 +08:00

181 lines
5.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 新增提醒功能实现文档
## 功能概述
基于现有的午餐提醒功能,新增了两个提醒功能:
1. **晚餐提醒**每天晚上6点提醒用户记录晚餐点击跳转到营养记录页面
2. **心情提醒**每天晚上9点提醒用户记录当日心情点击跳转到心情统计页面
## 实现细节
### 1. 通知帮助类扩展
#### 晚餐提醒功能 (`NutritionNotificationHelpers`)
新增方法:
- `scheduleDailyDinnerReminder(userName, hour=18, minute=0)`: 注册每日晚餐提醒
- `cancelDinnerReminder()`: 取消晚餐提醒
**特点:**
- 默认时间18:00晚上6点
- 提醒文案:`🍽️ 晚餐时光到啦!${userName},美好的晚餐时光开始了~记得记录今天的晚餐哦!营养均衡很重要呢 💪`
- 跳转链接:`/nutrition/records`
- 通知类型:`dinner_reminder`
#### 心情提醒功能 (`MoodNotificationHelpers`)
新增类和方法:
- `scheduleDailyMoodReminder(userName, hour=21, minute=0)`: 注册每日心情提醒
- `sendMoodReminder(userName)`: 发送即时心情提醒
- `cancelMoodReminder()`: 取消心情提醒
**特点:**
- 默认时间21:00晚上9点
- 提醒文案:`🌙 今天过得怎么样呀?${userName},夜深了~来记录一下今天的心情吧!每一份情感都值得被珍藏 ✨💕`
- 跳转链接:`/mood-statistics`
- 通知类型:`mood_reminder`
### 2. 应用启动注册
`app/_layout.tsx``Bootstrapper` 组件中:
```typescript
// 当用户数据加载完成且用户名存在时,注册所有提醒
React.useEffect(() => {
const registerAllReminders = async () => {
if (userDataLoaded && profile?.name) {
try {
await notificationService.initialize();
// 注册午餐提醒12:00
await NutritionNotificationHelpers.scheduleDailyLunchReminder(profile.name);
// 注册晚餐提醒18:00
await NutritionNotificationHelpers.scheduleDailyDinnerReminder(profile.name);
// 注册心情提醒21:00
await MoodNotificationHelpers.scheduleDailyMoodReminder(profile.name);
console.log('所有提醒已注册');
} catch (error) {
console.error('注册提醒失败:', error);
}
}
};
registerAllReminders();
}, [userDataLoaded, profile?.name]);
```
### 3. 通知点击处理
`services/notifications.ts` 中扩展了 `handleNotificationResponse` 方法:
```typescript
private handleNotificationResponse(response: Notifications.NotificationResponse): void {
const { notification } = response;
const data = notification.request.content.data;
// ... 其他处理逻辑
if (data?.type === 'dinner_reminder') {
// 处理晚餐提醒通知
console.log('用户点击了晚餐提醒通知', data);
// 跳转到营养记录页面
if (data?.url) {
router.push(data.url as string);
}
} else if (data?.type === 'mood_reminder') {
// 处理心情提醒通知
console.log('用户点击了心情提醒通知', data);
// 跳转到心情页面
if (data?.url) {
router.push(data.url as string);
}
}
}
```
### 4. 通知类型扩展
`NotificationTypes` 中新增:
```typescript
export const NotificationTypes = {
// ... 现有类型
DINNER_REMINDER: 'dinner_reminder',
MOOD_REMINDER: 'mood_reminder',
} as const;
```
## 用户体验设计
### 1. 提醒文案设计原则
- **可爱生动**使用emoji和亲切的语言
- **个性化**:包含用户名称
- **激励性**:鼓励用户养成良好习惯
- **情感化**:让用户感受到关怀
### 2. 时间安排
- **午餐提醒**12:00 - 用餐高峰期
- **晚餐提醒**18:00 - 晚餐准备时间
- **心情提醒**21:00 - 一天结束,适合反思
### 3. 跳转逻辑
- **营养提醒**:直接跳转到营养记录页面,方便用户快速记录
- **心情提醒**:跳转到心情统计页面,用户可以查看历史并添加新记录
## 技术特点
### 1. 防重复注册
- 每个提醒类型都会检查是否已存在相同的提醒
- 避免重复注册导致的多次通知
### 2. 错误处理
- 完整的try-catch错误处理
- 详细的日志记录
- 优雅的降级处理
### 3. 类型安全
- 完整的TypeScript类型定义
- 通知数据结构的类型约束
### 4. 可扩展性
- 模块化的设计
- 易于添加新的提醒类型
- 统一的接口规范
## 测试建议
### 1. 功能测试
- [ ] 验证提醒是否在正确时间触发
- [ ] 测试通知点击跳转是否正确
- [ ] 检查防重复注册机制
- [ ] 验证用户名个性化显示
### 2. 用户体验测试
- [ ] 提醒文案是否吸引人
- [ ] 跳转页面是否符合预期
- [ ] 通知频率是否合适
- [ ] 整体用户流程是否顺畅
### 3. 边界情况测试
- [ ] 用户名为空的处理
- [ ] 权限被拒绝的处理
- [ ] 应用被杀死后的提醒恢复
- [ ] 系统时间变更的影响
## 后续优化建议
1. **个性化时间设置**:允许用户自定义提醒时间
2. **智能提醒**:根据用户习惯调整提醒时间
3. **提醒开关**:允许用户单独控制每种提醒
4. **提醒统计**:记录用户对提醒的响应情况
5. **A/B测试**:测试不同文案的效果
## 相关文件
- `utils/notificationHelpers.ts` - 提醒功能实现
- `app/_layout.tsx` - 应用启动时的提醒注册
- `services/notifications.ts` - 通知服务和点击处理
- `app/nutrition/records.tsx` - 营养记录页面
- `app/mood-statistics.tsx` - 心情统计页面