This article aims to deeply explore the technical details of Huawei's HarmonyOS Next system (as of API 12 currently) and is summarized based on actual development practices.
It is mainly used as a carrier for technical sharing and exchange. Inevitable errors and omissions may exist. Colleagues are welcome to put forward valuable opinions and questions for common progress.
This article is original content. Any form of reprint must indicate the source and original author.
I. Project Background and Demand Analysis
The smart reminder assistant is an application for various reminder scenarios, used to reliably remind users of various events in lock screen or background state. Typical scenarios include:
- Calendar event reminder: Used to remind users of important schedules.
- Countdown reminder: Users can customize countdowns for specific reminders.
- Smart alarm clock: An alarm clock function that is automatically triggered at a specified time.
To ensure the stability and energy-saving effect of the application when running in the background, we will combine the agent-powered reminder and deferred task of HarmonyOS Next to realize the background scheduling of various reminders.
II. Technical Requirements and Challenges
To ensure the user experience of the reminder assistant and the utilization rate of system resources, we need to solve the following technical challenges in implementation:
- Management of multiple reminder types: Need to support calendar reminders, countdowns, and alarm clocks, and be able to manage tasks in the background.
- Optimization when system resources are limited: When system resources are limited or memory is insufficient, the reminder tasks can be scheduled and optimized according to user habits to reduce the impact on device power and performance.
III. Design Ideas
1. Task decomposition: Divide modules by different reminder types
The smart reminder assistant will be decomposed into the following modules according to reminder types:
- Calendar reminder module: Trigger and display reminder notifications based on user schedules.
- Countdown reminder module: Trigger reminders when the countdown ends and display through notifications.
- Smart alarm clock module: Trigger alarm clock reminders at the set time.
Each module will use the reminder function provided by agent-powered reminder
to maintain stable reminder notifications in lock screen or background state.
2. Selection of background task types
According to the characteristics of HarmonyOS Next's background task management, we will use the following task types:
-
Agent-powered reminder: Agent-powered reminders have the ability of system agent reminders and are suitable for countdown, calendar, and alarm clock reminder scenarios. By setting different
ReminderRequest
instance types, we can flexibly implement various reminder functions. - Deferred task: For reminder tasks that do not need to be triggered immediately (such as regular schedule updates), deferred tasks can be used to reduce resource occupation.
3. System resource optimization: Use NotificationSlot to optimize notification experience
Agent-powered reminders not only provide notification functions when locked or in the background, but also support multiple notification configurations. By configuring NotificationSlot, custom buttons, alarm clock volume, vibration and other effects can be added to reminder notifications. At the same time, the frequency of tasks can be dynamically scheduled according to system resource conditions to optimize the reminder experience.
IV. Key Technology Implementation
1. Implementation and parameter configuration of agent-powered reminders for each reminder type
In agent-powered reminders, according to the different needs of reminder types, we will create ReminderRequest
instances of countdown, calendar, and alarm clock types respectively. The following are the parameter configurations of each reminder type:
Reminder type | Configuration parameter | Description |
---|---|---|
Countdown reminder | triggerTimeInSeconds |
Trigger time (in seconds) |
Calendar reminder | dateTime |
Trigger date and time |
Alarm clock reminder |
hour , minute , daysOfWeek
|
Trigger time and repetition period |
Code example: Implementation of countdown reminder
import { reminderAgentManager } from '@kit.BackgroundTasksKit';
// Define countdown reminder instance
let countdownReminder = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER,
triggerTimeInSeconds: 300, // 5-minute countdown
title: 'Countdown reminder',
content: 'Time is up!',
expiredContent: 'Reminder has expired',
notificationId: 101,
actionButton: [
{ title: 'Close', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE }
],
wantAgent: {
pkgName: 'com.example.app',
abilityName: 'MainAbility'
}
};
// Publish countdown reminder
reminderAgentManager.publishReminder(countdownReminder)
.then(res => console.info(`Countdown reminder published successfully, ID: ${res}`))
.catch(err => console.error(`Countdown reminder publication failed, error code: ${err.code}, message: ${err.message}`));
Code example: Implementation of calendar reminder
import { reminderAgentManager } from '@kit.BackgroundTasksKit';
// Define calendar reminder instance
let calendarReminder = {
reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR,
dateTime: { year: 2024, month: 11, day: 30, hour: 9, minute: 0, second: 0 },
title: 'Meeting reminder',
content: 'You have a meeting about to start',
expiredContent: 'Meeting reminder has expired',
notificationId: 102,
actionButton: [
{ title: 'Close', type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE }
]
};
// Publish calendar reminder
reminderAgentManager.publishReminder(calendarReminder)
.then(res => console.info(`Calendar reminder published successfully, ID: ${res}`))
.catch(err => console.error(`Calendar reminder publication failed, error code: ${err.code}, message: ${err.message}`));
2. System resource optimization: Group management of reminder task frequency
In terms of background task frequency control, HarmonyOS Next divides tasks into different activity groups according to the application usage frequency. For example, in the "frequently used" group, the minimum interval of reminder tasks is 2 hours, while in the "rarely used" group it is 48 hours. For each type of reminder, we can use this strategy to control frequency and reduce resource consumption.
Activity group | Minimum interval time |
---|---|
Active group | 2 hours |
Frequently used | 4 hours |
Commonly used | 24 hours |
Rarely used | 48 hours |
V. Summary and Extension
Through the above design and implementation, we have built a smart assistant with multi-scenario reminder functions such as countdown, calendar events, and alarm clocks, and ensured the stability and energy-saving effect of the system in terms of resource optimization and background task scheduling. In the future, we can consider further expanding functions:
- Extend more reminder types: For example, weather reminders, news reminders, etc., flexibly support more intelligent scenarios through agent-powered reminders.
- Optimize reminder frequency: Dynamically analyze user usage habits and adjust the trigger frequency of reminder tasks according to different activity groups to further improve the efficiency of system resource management.
This smart reminder assistant realizes multi-scenario reminder functions through the powerful background task management capabilities of HarmonyOS Next, while taking into account user experience and system resource usage efficiency.