
回复
嗨~我是小L!鸿蒙代理提醒能让应用关闭后仍准时触发通知。今天用3个核心点,带你快速掌握倒计时、日历、闹钟三类提醒的开发技巧~
类型 | 触发方式 | 典型场景 | 关键参数 |
---|---|---|---|
倒计时提醒 | 相对时间(秒) | 会议倒计时、秒杀提醒 | triggerTimeInSeconds |
日历提醒 | 绝对日期时间 | 生日、还款提醒 | dateTime /repeatMonths |
闹钟提醒 | 每日固定时间 | 起床闹钟、服药提醒 | hour /daysOfWeek |
代码示例:倒计时提醒
const timerReminder = {
reminderType: 'TIMER',
triggerTimeInSeconds: 300, // 5分钟后提醒
actionButton: [{ title: '查看', type: 'CLOSE' }]
};
reminderAgentManager.publishReminder(timerReminder);
// module.json5
{
"reqPermissions": [
{ "name": "ohos.permission.PUBLISH_AGENT_REMINDER" }
]
}
const alarmReminder = {
reminderType: 'ALARM',
hour: 7,
minute: 30,
daysOfWeek: [1,2,3,4,5], // 工作日
snoozeTime: 600, // 贪睡10分钟
};
// 发布提醒
reminderAgentManager.publishReminder(alarmReminder).then(id => {
console.log('提醒ID:', id);
});
// 取消提醒
reminderAgentManager.cancelReminder(id);
const slot = {
slotId: 'urgent',
name: '重要提醒',
importance: 'HIGH', // 弹窗+响铃
ledColor: '#FF0000'
};
notificationManager.addNotificationSlot(slot);
const buttons = [
{ title: '处理', type: 'ACTION', want: { abilityName: 'TaskAbility' } },
{ title: '延后', type: 'SNOOZE', snoozeTime: 3600 }
];
主题:【代理提醒权限申请】-应用名-包名
正文要点:
申请用于「健身App」每日运动提醒,用户可设置每周1-3次,点击通知直接跳转训练页面。
daysOfWeek
等参数缩小触发范围