基于鸿蒙代理提醒与延迟任务,打造低耗稳定的提醒系统,核心逻辑如下:

一、三大提醒类型:按需触发⏰

| 类型 | 核心场景 | 关键参数 | 代码示例片段 |
|------------|-------------------------|---------------------------|-------------------------------|
| 倒计时提醒 | 会议倒计时、烹饪定时 | triggerTimeInSeconds | publishReminder({type:'TIMER', trigger:300}) |
| 日历提醒 | 生日、还款、会议预约 | dateTime/repeatMonths | dateTime:{year:2024, month:12, day:31} |
| 闹钟提醒 | 每日起床、服药提醒 | hour/daysOfWeek | daysOfWeek:[1,2,3,4,5](工作日) |

统一发布接口

function scheduleReminder(reminder: ReminderRequestBase) {  
  reminderAgentManager.publishReminder(reminder).then(id => {  
    console.log('提醒ID:', id); // 存储ID用于管理  
  });  
}  

二、后台任务优化:低耗与稳定兼顾⚡

1. 系统分组调度策略

| 应用活跃程度 | 提醒最小间隔 | 适用场景 |
|----------------|--------------|------------------------|
| 活跃(如社交) | 2小时 | 即时消息通知 |
| 常用(如工具) | 24小时 | 定期数据同步 |
| 极少使用 | 48小时 | 低频清理提醒 |

2. 动态资源适配

  • 低电量模式:电量<15%时暂停非紧急提醒

    batteryManager.on('levelChanged', (level) => {  
      if (level < 15) cancelAllReminders();  
    });  
  • 内存不足时:优先保留高优先级提醒(如闹钟)

    const highPriorityReminder = { priority: 'HIGH', overridePolicy: true };  

三、通知体验优化:交互与美观并重✨

1. 自定义通知渠道

const reminderSlot = {  
  slotId: 'alarm_channel',  
  name: '重要提醒',  
  importance: 'HIGH', // 弹窗+响铃+呼吸灯  
  vibrationPattern: [100, 200], // 振动节奏(毫秒)  
  ledColor: '#FF5733'  
};  
notificationManager.addNotificationSlot(reminderSlot);  

2. 交互式按钮设计

const buttons = [  
  {  
    title: '立即处理',  
    type: 'ACTION',  
    want: { abilityName: 'TaskDetailAbility' } // 点击跳转  
  },  
  {  
    title: '1小时后提醒',  
    type: 'SNOOZE',  
    snoozeTime: 3600 // 延时执行  
  }  
];  

四、权限申请与合规📧

邮件模板速记

  • 主题:【代理提醒权限】-应用名-包名
  • 正文要点:
    ✅ 场景:如「医疗App每日3次用药提醒」
    ✅ 频率:承诺「单用户每日≤5条」
    ✅ 关闭入口:「设置页提供一键关闭」

示例

申请用于「健身助手」的每日运动提醒,用户可设置每周3次,点击通知直达训练计划。

总结:核心能力图谱

graph LR  
A[用户设置提醒] --> B{类型判断}  
B -->|倒计时| C[TIMER类型代理提醒]  
B -->|日历事件| D[CALENDAR类型代理提醒]  
B -->|每日闹钟| E[ALARM类型代理提醒]  
CDE --> F[系统分组调度]  
F --> G[动态资源适配]  

lyc233333
1 声望0 粉丝