嗨~我是小L!鸿蒙代理提醒能让应用关闭后仍准时触发通知。今天用3个核心点,带你快速掌握倒计时、日历、闹钟三类提醒的开发技巧~

一、三类提醒:按需选择「时间触发器」⏰

| 类型 | 触发方式 | 典型场景 | 关键参数 |
|------------|----------------|-------------------------|---------------------------|
| 倒计时提醒 | 相对时间(秒) | 会议倒计时、秒杀提醒 | triggerTimeInSeconds |
| 日历提醒 | 绝对日期时间 | 生日、还款提醒 | dateTime/repeatMonths |
| 闹钟提醒 | 每日固定时间 | 起床闹钟、服药提醒 | hour/daysOfWeek |

代码示例:倒计时提醒

const timerReminder = {  
  reminderType: 'TIMER',  
  triggerTimeInSeconds: 300, // 5分钟后提醒  
  actionButton: [{ title: '查看', type: 'CLOSE' }]  
};  
reminderAgentManager.publishReminder(timerReminder);  

二、开发流程:权限→配置→发布「三步曲」🚀

1. 申请权限(必加!)

// module.json5  
{  
  "reqPermissions": [  
    { "name": "ohos.permission.PUBLISH_AGENT_REMINDER" }  
  ]  
}  

2. 配置提醒内容

const alarmReminder = {  
  reminderType: 'ALARM',  
  hour: 7,  
  minute: 30,  
  daysOfWeek: [1,2,3,4,5], // 工作日  
  snoozeTime: 600, // 贪睡10分钟  
};  

3. 发布与管理

// 发布提醒  
reminderAgentManager.publishReminder(alarmReminder).then(id => {  
  console.log('提醒ID:', id);  
});  

// 取消提醒  
reminderAgentManager.cancelReminder(id);  

三、通知优化:让提醒更「醒目」👀

1. 自定义通知渠道

const slot = {  
  slotId: 'urgent',  
  name: '重要提醒',  
  importance: 'HIGH', // 弹窗+响铃  
  ledColor: '#FF0000'  
};  
notificationManager.addNotificationSlot(slot);  

2. 交互式按钮

const buttons = [  
  { title: '处理', type: 'ACTION', want: { abilityName: 'TaskAbility' } },  
  { title: '延后', type: 'SNOOZE', snoozeTime: 3600 }  
];  

四、权限申请:邮件模板速查表📧

主题:【代理提醒权限申请】-应用名-包名
正文要点

  • 场景:如「医疗App用药提醒,每日3次」
  • 频率:承诺「单个用户每日≤5条」
  • 关闭入口:说明「设置页可一键关闭」

示例

申请用于「健身App」每日运动提醒,用户可设置每周1-3次,点击通知直接跳转训练页面。

总结:提醒设计「三要」

  • 要精准:选对类型,用daysOfWeek等参数缩小触发范围
  • 要克制:避免频繁推送,低电量时自动暂停
  • 要交互:提供「处理/延后」按钮,提升用户参与度

lyc233333
1 声望0 粉丝