1e41f4b71Sopenharmony_ci# Agent-powered Reminder (ArkTS) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### Introduction 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciAfter an application switches to the background or an application process is terminated, it may have scheduled tasks for reminding users, for example, flash sale reminders for shopping applications. To meet this requirement, the system provides agent-powered reminders (implemented by **reminderAgentManager**). When the application switches to the background or the process is terminated, the system sends reminders on behalf of the application. Currently, the following reminder types are supported: timer, calendar, and alarm.<!--RP1--><!--RP1End--> 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci- Timer: reminders based on countdown timers 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- Calendar: reminders based on calendar events 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci- Alarm: reminders based on alarm clocks 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### Constraints 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci- **Quantity limit**: A third-party application supports a maximum of 30 valid reminders. A system application supports a maximum of 10,000 valid reminders. The entire system supports a maximum of 12,000 valid reminders. (A reminder is considered valid as long as it is published.) 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- **Redirection limit**: The application that is redirected to upon a click on the notification must be the application that requested the agent-powered reminder. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci<!--RP2--><!--RP2End--> 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci## Available APIs 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciThe table below uses promise as an example to describe the APIs used for developing agent-powered reminders. For details about more APIs and their usage, see [reminderAgentManager](../reference/apis-backgroundtasks-kit/js-apis-reminderAgentManager.md). 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**Table 1** Main APIs for agent-powered reminders 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci| API| Description| 31e41f4b71Sopenharmony_ci| -------- | -------- | 32e41f4b71Sopenharmony_ci| publishReminder(reminderReq: ReminderRequest): Promise<number> | Publishes a reminder.| 33e41f4b71Sopenharmony_ci| cancelReminder(reminderId: number): Promise<void> | Cancels a reminder.| 34e41f4b71Sopenharmony_ci| getValidReminders(): Promise<Array<ReminderRequest>> | Obtains all valid reminders set by the current application.| 35e41f4b71Sopenharmony_ci| cancelAllReminders(): Promise<void> | Cancels all reminders set by the current application.| 36e41f4b71Sopenharmony_ci| addNotificationSlot(slot: NotificationSlot): Promise<void> | Adds a notification slot.| 37e41f4b71Sopenharmony_ci| removeNotificationSlot(slotType: notification.SlotType): Promise<void> | Removes a notification slot.| 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci## How to Develop 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci1. Declare the **ohos.permission.PUBLISH_AGENT_REMINDER** permission. For details, see [Declaring Permissions](../security/AccessToken/declare-permissions.md). 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci2. [Request notification authorization](../notification/notification-enable.md). Agent-powered reminders can be used only after being authorized by the user. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci3. Import the modules. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci ```ts 49e41f4b71Sopenharmony_ci import { reminderAgentManager } from '@kit.BackgroundTasksKit'; 50e41f4b71Sopenharmony_ci import { notificationManager } from '@kit.NotificationKit'; 51e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 52e41f4b71Sopenharmony_ci ``` 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci4. Define a reminder. You can define the following types of reminders based on project requirements. 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci - Timer 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci ```ts 59e41f4b71Sopenharmony_ci let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = { 60e41f4b71Sopenharmony_ci reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER, // The reminder type is timer. 61e41f4b71Sopenharmony_ci triggerTimeInSeconds: 10, 62e41f4b71Sopenharmony_ci actionButton: [ // Set the button type and title displayed for the reminder in the notification panel. 63e41f4b71Sopenharmony_ci { 64e41f4b71Sopenharmony_ci title: 'close', 65e41f4b71Sopenharmony_ci type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci ], 68e41f4b71Sopenharmony_ci wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched. 69e41f4b71Sopenharmony_ci pkgName: 'com.example.myapplication', 70e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 71e41f4b71Sopenharmony_ci }, 72e41f4b71Sopenharmony_ci maxScreenWantAgent: { // Information about the target UIAbility that is automatically started when the specified reminder time arrives is displayed in full screen. 73e41f4b71Sopenharmony_ci pkgName: 'com.example.myapplication', 74e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 75e41f4b71Sopenharmony_ci }, 76e41f4b71Sopenharmony_ci title: 'this is title', // Reminder title. 77e41f4b71Sopenharmony_ci content: 'this is content', // Reminder content. 78e41f4b71Sopenharmony_ci expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 79e41f4b71Sopenharmony_ci notificationId: 100, // Notification ID used by the reminder. If there are reminders with the same notification ID, the later one will overwrite the earlier one. 80e41f4b71Sopenharmony_ci slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci ``` 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci - Calendar 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci ```ts 87e41f4b71Sopenharmony_ci let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = { 88e41f4b71Sopenharmony_ci reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // The reminder type is calendar. 89e41f4b71Sopenharmony_ci dateTime: { // Reminder time. 90e41f4b71Sopenharmony_ci year: 2023, 91e41f4b71Sopenharmony_ci month: 1, 92e41f4b71Sopenharmony_ci day: 1, 93e41f4b71Sopenharmony_ci hour: 11, 94e41f4b71Sopenharmony_ci minute: 14, 95e41f4b71Sopenharmony_ci second: 30 96e41f4b71Sopenharmony_ci }, 97e41f4b71Sopenharmony_ci repeatMonths: [1], // Month in which the reminder repeats. 98e41f4b71Sopenharmony_ci repeatDays: [1], // Date on which the reminder repeats. 99e41f4b71Sopenharmony_ci actionButton: [ // Set the button type and title displayed for the reminder in the notification panel. 100e41f4b71Sopenharmony_ci { 101e41f4b71Sopenharmony_ci title: 'close', 102e41f4b71Sopenharmony_ci type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 103e41f4b71Sopenharmony_ci }, 104e41f4b71Sopenharmony_ci { 105e41f4b71Sopenharmony_ci title: 'snooze', 106e41f4b71Sopenharmony_ci type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE 107e41f4b71Sopenharmony_ci }, 108e41f4b71Sopenharmony_ci ], 109e41f4b71Sopenharmony_ci wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched. 110e41f4b71Sopenharmony_ci pkgName: 'com.example.myapplication', 111e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 112e41f4b71Sopenharmony_ci }, 113e41f4b71Sopenharmony_ci maxScreenWantAgent: { // Information about the target UIAbility that is automatically started when the specified reminder time arrives is displayed in full screen. 114e41f4b71Sopenharmony_ci pkgName: 'com.example.myapplication', 115e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 116e41f4b71Sopenharmony_ci }, 117e41f4b71Sopenharmony_ci ringDuration: 5, // Ringing duration, in seconds. 118e41f4b71Sopenharmony_ci snoozeTimes: 2, // Number of reminder snooze times. 119e41f4b71Sopenharmony_ci timeInterval: 5*60, // Reminder snooze interval, in seconds. 120e41f4b71Sopenharmony_ci title: 'this is title', // Reminder title. 121e41f4b71Sopenharmony_ci content: 'this is content', // Reminder content. 122e41f4b71Sopenharmony_ci expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 123e41f4b71Sopenharmony_ci snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed. 124e41f4b71Sopenharmony_ci notificationId: 100, // Notification ID used by the reminder. If there are reminders with the same notification ID, the later one will overwrite the earlier one. 125e41f4b71Sopenharmony_ci slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 126e41f4b71Sopenharmony_ci } 127e41f4b71Sopenharmony_ci ``` 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci - Alarm 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci ```ts 132e41f4b71Sopenharmony_ci let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = { 133e41f4b71Sopenharmony_ci reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // The reminder type is alarm. 134e41f4b71Sopenharmony_ci hour: 23, // Hour portion of the reminder time. 135e41f4b71Sopenharmony_ci minute: 9, // Minute portion of the reminder time. 136e41f4b71Sopenharmony_ci daysOfWeek: [2], // Days of a week when the reminder repeats.. 137e41f4b71Sopenharmony_ci actionButton: [ // Set the button type and title displayed for the reminder in the notification panel. 138e41f4b71Sopenharmony_ci { 139e41f4b71Sopenharmony_ci title: 'close', 140e41f4b71Sopenharmony_ci type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 141e41f4b71Sopenharmony_ci }, 142e41f4b71Sopenharmony_ci { 143e41f4b71Sopenharmony_ci title: 'snooze', 144e41f4b71Sopenharmony_ci type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE 145e41f4b71Sopenharmony_ci }, 146e41f4b71Sopenharmony_ci ], 147e41f4b71Sopenharmony_ci wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched. 148e41f4b71Sopenharmony_ci pkgName: 'com.example.myapplication', 149e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 150e41f4b71Sopenharmony_ci }, 151e41f4b71Sopenharmony_ci maxScreenWantAgent: { // Information about the target UIAbility that is automatically started when the specified reminder time arrives is displayed in full screen. 152e41f4b71Sopenharmony_ci pkgName: 'com.example.myapplication', 153e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 154e41f4b71Sopenharmony_ci }, 155e41f4b71Sopenharmony_ci ringDuration: 5, // Ringing duration, in seconds. 156e41f4b71Sopenharmony_ci snoozeTimes: 2, // Number of reminder snooze times. 157e41f4b71Sopenharmony_ci timeInterval: 5*60, // Reminder snooze interval, in seconds. 158e41f4b71Sopenharmony_ci title: 'this is title', // Reminder title. 159e41f4b71Sopenharmony_ci content: 'this is content', // Reminder content. 160e41f4b71Sopenharmony_ci expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 161e41f4b71Sopenharmony_ci snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed. 162e41f4b71Sopenharmony_ci notificationId: 99, // Notification ID used by the reminder. If there are reminders with the same notification ID, the later one will overwrite the earlier one. 163e41f4b71Sopenharmony_ci slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci ``` 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci5. Publish the reminder. After the reminder is published, your application can use the agent-powered reminder feature. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci ```ts 170e41f4b71Sopenharmony_ci reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => { 171e41f4b71Sopenharmony_ci console.info('Succeeded in publishing reminder. '); 172e41f4b71Sopenharmony_ci let reminderId: number = res; // ID of the published reminder. 173e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 174e41f4b71Sopenharmony_ci console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`); 175e41f4b71Sopenharmony_ci }) 176e41f4b71Sopenharmony_ci ``` 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci6. Delete the reminder as required. 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci ```ts 181e41f4b71Sopenharmony_ci let reminderId: number = 1; 182e41f4b71Sopenharmony_ci // The reminder ID is obtained from the callback after the reminder is published. 183e41f4b71Sopenharmony_ci reminderAgentManager.cancelReminder(reminderId).then(() => { 184e41f4b71Sopenharmony_ci console.log('Succeeded in canceling reminder.'); 185e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 186e41f4b71Sopenharmony_ci console.error(`Failed to cancel reminder. Code: ${err.code}, message: ${err.message}`); 187e41f4b71Sopenharmony_ci }); 188e41f4b71Sopenharmony_ci ``` 189e41f4b71Sopenharmony_ci 190