后台提醒与代理提醒:HarmonyOS Next 的智能提醒管理
SameX
Posted on November 2, 2024
本文旨在深入探讨华为鸿蒙HarmonyOS Next系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。
在移动应用开发中,及时有效的提醒功能对于提升用户体验至关重要。HarmonyOS Next 提供了代理提醒(Agent-powered Reminder)功能,它允许应用在后台被挂起或进程终止后,由系统代理应用执行提醒任务,例如倒计时、日历、闹钟等。这种机制可以确保用户及时收到重要的提醒信息,同时避免应用过度消耗设备资源。
代理提醒的类型与开发步骤
HarmonyOS Next 支持三种类型的代理提醒:
- 倒计时提醒:基于倒计时的提醒功能,例如倒计时 10 秒后提醒用户。
- 日历提醒:基于日历事件的提醒功能,例如在特定日期和时间提醒用户。
- 闹钟提醒:基于时钟的提醒功能,例如每天早上 7 点提醒用户起床。 开发步骤:
-
申请权限:在应用配置文件中添加
ohos.permission.PUBLISH_AGENT_REMINDER
权限。 - 请求通知授权:获得用户授权后,才能使用代理提醒功能。
- 定义提醒内容:根据需要定义倒计时、日历或闹钟提醒内容,包括提醒标题、内容、过期内容、通知渠道等。
-
发布提醒:调用
publishReminder
接口发布提醒任务。 -
取消提醒:根据需要调用
cancelReminder
或cancelAllReminders
接口取消提醒任务。 ### 示例代码:倒计时提醒、日历提醒、闹钟提醒的设置 以下代码示例展示了如何设置三种类型的代理提醒: 倒计时提醒:
import { reminderAgentManager } from '@kit.BackgroundTasksKit';
import { notificationManager } from '@kit.NotificationKit';
let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,
triggerTimeInSeconds: 10,
actionButton: [{ title: 'close', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE }],
wantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
maxScreenWantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
title: 'this is title',
content: 'this is content',
expiredContent: 'this reminder has expired',
notificationId: 100,
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
console.info('Succeeded in publishing reminder. ');
let reminderId: number = res; // 发布的提醒 ID
}).catch((err: BusinessError) => {
console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
});
日历提醒:
import { reminderAgentManager } from '@kit.BackgroundTasksKit';
import { notificationManager } from '@kit.NotificationKit';
let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR,
dateTime: {
year: 2023,
month: 1,
day: 1,
hour: 11,
minute: 14,
second: 30
},
repeatMonths: [1],
repeatDays: [1],
actionButton: [
{ title: 'close', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE },
{ title: 'snooze', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE }
],
wantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
maxScreenWantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
ringDuration: 5,
snoozeTimes: 2,
timeInterval: 5 * 60,
title: 'this is title',
content: 'this is content',
expiredContent: 'this reminder has expired',
snoozeContent: 'remind later',
notificationId: 100,
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
console.info('Succeeded in publishing reminder. ');
let reminderId: number = res; // 发布的提醒 ID
}).catch((err: BusinessError) => {
console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
});
闹钟提醒:
import { reminderAgentManager } from '@kit.BackgroundTasksKit';
import { notificationManager } from '@kit.NotificationKit';
let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM,
hour: 23,
minute: 9,
daysOfWeek: [2],
actionButton: [
{ title: 'close', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE },
{ title: 'snooze', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE }
],
wantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
maxScreenWantAgent: { pkgName: 'com.example.myapplication', abilityName: 'EntryAbility' },
ringDuration: 5,
snoozeTimes: 2,
timeInterval: 5 * 60,
title: 'this is title',
content: 'this is content',
expiredContent: 'this reminder has expired',
snoozeContent: 'remind later',
notificationId: 99,
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
console.info('Succeeded in publishing reminder. ');
let reminderId: number = res; // 发布的提醒 ID
}).catch((err: BusinessError) => {
console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
});
表格:代理提醒类型对比
提醒类型 | 触发方式 | 重复设置 | 通知按钮 | 适用场景 |
---|---|---|---|---|
倒计时提醒 | 倒计时结束 | 不支持 | 关闭 | 临时提醒,例如会议倒计时 |
日历提醒 | 指定日期和时间 | 支持按月或按日重复 | 关闭、延时 | 定期提醒,例如生日、纪念日 |
闹钟提醒 | 指定时间 | 支持按周重复 | 关闭、延时 | 每日提醒,例如起床闹钟 |
提醒的通知管理与优化
开发者可以使用 NotificationSlot
来管理提醒通知的样式和渠道。通过设置不同的 NotificationSlot
,开发者可以创建个性化、多样化的通知样式,并选择合适的渠道进行通知,例如系统通知栏、桌面小组件等。
代码示例:
import { notificationManager } from '@kit.NotificationKit';
let slot: notificationManager.Slot = {
slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
slotId: 1,
name: 'My Notification Slot',
importance: notificationManager.Importance.HIGH,
description: 'This is my custom notification slot'
};
notificationManager.addNotificationSlot(slot).then(() => {
console.info('Notification slot added successfully');
}).catch((err: BusinessError) => {
console.error(`Failed to add notification slot. Code: ${err.code}, message: ${err.message}`);
});
代理提醒权限的申请方法
为了防止代理提醒功能被滥用,HarmonyOS Next 对其进行了限制和规范。开发者需要向华为官方申请代理提醒权限,才能使用该功能。
申请方法:
- 通过
hwpush@huawei.com
邮箱向华为官方申请。 - 邮件主题:
【代理提醒权限申请】
- 邮件正文:包含企业名称、应用名称、应用包名、使用场景、通知标题、通知文本、通知场景、通知频率等信息。
### 总结
代理提醒为 HarmonyOS Next 提供了一种智能的提醒管理方式,它可以有效地提升用户体验,并避免应用过度消耗设备资源。咱们可以根据实际需求选择合适的代理提醒类型,并结合
NotificationSlot
进行通知管理和优化。同时,咱们也需要注意代理提醒的权限申请和使用规范,避免滥用该功能。
💖 💪 🙅 🚩
SameX
Posted on November 2, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
harmonyos HarmonyOS Next Security: Application Encryption - Ensuring the Security of Application Code
November 29, 2024
harmonyos HarmonyOS Next in Practice: Building a Secure and Efficient Online Payment Application
November 29, 2024
harmonyos In-depth Exploration of the HarmonyOS Next Password Autofill Service: Advanced Features and Adaptation Scenarios
November 29, 2024
harmonyos HarmonyOS Next in Practice: Building a New Ecosystem for Social Applications
November 29, 2024