1# Agent-powered Reminder 2 3## Overview 4 5### Introduction 6 7After 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. 8 9- Timer: reminders based on countdown timers 10 11- Calendar: reminders based on calendar events 12 13- Alarm: reminders based on alarm clocks 14 15### Constraints 16 17- **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.) 18 19- **Redirection limit**: The application that is redirected to upon a click on the notification must be the application that requested the agent-powered reminder. 20 21 22## Available APIs 23 24The 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/js-apis-reminderAgentManager.md). 25 26**Table 1** Main APIs for agent-powered reminders 27 28| API| Description| 29| -------- | -------- | 30| publishReminder(reminderReq: ReminderRequest): Promise<number> | Publishes a reminder.| 31| cancelReminder(reminderId: number): Promise<void> | Cancels a reminder.| 32| getValidReminders(): Promise<Array<ReminderRequest>> | Obtains all valid reminders set by the current application.| 33| cancelAllReminders(): Promise<void> | Cancels all reminders set by the current application.| 34| addNotificationSlot(slot: NotificationSlot): Promise<void> | Adds a notification slot.| 35| removeNotificationSlot(slotType: notification.SlotType): Promise<void> | Removes a notification slot.| 36 37 38## How to Develop 39 401. Request the **ohos.permission.PUBLISH_AGENT_REMINDER** permission. For details, see [Declaring Permissions in the Configuration File](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file). 41 422. [Enable the notification feature](../notification/notification-enable.md). Agent-powered reminders can be used only after being authorized by the user. 43 443. Import the modules. 45 46 ```js 47 import reminderAgentManager from '@ohos.reminderAgentManager'; 48 import notificationManager from '@ohos.notificationManager'; 49 ``` 50 514. Define a reminder. You can define the following types of reminders based on project requirements. 52 53 - Timer 54 55 ```js 56 let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = { 57 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER, // The reminder type is timer. 58 triggerTimeInSeconds: 10, 59 actionButton: [ // Set the button type and title displayed for the reminder in the notification panel. 60 { 61 title: 'close', 62 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 63 } 64 ], 65 wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched. 66 pkgName: 'com.example.myapplication', 67 abilityName: 'EntryAbility' 68 }, 69 maxScreenWantAgent: { // Information about the target UIAbility that is automatically started when the specified reminder time arrives is displayed in full screen. 70 pkgName: 'com.example.myapplication', 71 abilityName: 'EntryAbility' 72 }, 73 title: 'this is title', // Reminder title. 74 content: 'this is content', // Reminder content. 75 expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 76 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. 77 slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 78 } 79 ``` 80 81 - Calendar 82 83 ```js 84 let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = { 85 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // The reminder type is calendar. 86 dateTime: { // Reminder time. 87 year: 2023, 88 month: 1, 89 day: 1, 90 hour: 11, 91 minute: 14, 92 second: 30 93 }, 94 repeatMonths: [1], // Month in which the reminder repeats. 95 repeatDays: [1], // Date on which the reminder repeats. 96 actionButton: [ // Set the button type and title displayed for the reminder in the notification panel. 97 { 98 title: 'close', 99 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 100 }, 101 { 102 title: 'snooze', 103 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE 104 }, 105 ], 106 wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched. 107 pkgName: 'com.example.myapplication', 108 abilityName: 'EntryAbility' 109 }, 110 maxScreenWantAgent: { // Information about the target UIAbility that is automatically started when the specified reminder time arrives is displayed in full screen. 111 pkgName: 'com.example.myapplication', 112 abilityName: 'EntryAbility' 113 }, 114 ringDuration: 5, // Ringing duration, in seconds. 115 snoozeTimes: 2, // Number of reminder snooze times. 116 timeInterval: 5, // Reminder snooze interval, in seconds. 117 title: 'this is title', // Reminder title. 118 content: 'this is content', // Reminder content. 119 expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 120 snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed. 121 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. 122 slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 123 } 124 ``` 125 126 - Alarm 127 128 ```js 129 let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = { 130 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // The reminder type is alarm. 131 hour: 23, // Hour portion of the reminder time. 132 minute: 9, // Minute portion of the reminder time. 133 daysOfWeek: [2], // Days of a week when the reminder repeats.. 134 actionButton: [ // Set the button type and title displayed for the reminder in the notification panel. 135 { 136 title: 'close', 137 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 138 }, 139 { 140 title: 'snooze', 141 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE 142 }, 143 ], 144 wantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched. 145 pkgName: 'com.example.myapplication', 146 abilityName: 'EntryAbility' 147 }, 148 maxScreenWantAgent: { // Information about the target UIAbility that is automatically started when the specified reminder time arrives is displayed in full screen. 149 pkgName: 'com.example.myapplication', 150 abilityName: 'EntryAbility' 151 }, 152 ringDuration: 5, // Ringing duration, in seconds. 153 snoozeTimes: 2, // Number of reminder snooze times. 154 timeInterval: 5, // Reminder snooze interval, in seconds. 155 title: 'this is title', // Reminder title. 156 content: 'this is content', // Reminder content. 157 expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 158 snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed. 159 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. 160 slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 161 } 162 ``` 163 1645. Publish the reminder. After the reminder is published, your application can use the agent-powered reminder feature. 165 166 ```js 167 try { 168 reminderAgentManager.publishReminder(targetReminderAgent).then(res => { 169 console.info('Succeeded in publishing reminder. '); 170 let reminderId: number = res; // ID of the published reminder. 171 }).catch(err => { 172 console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`); 173 }) 174 } catch (err) { 175 console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`); 176 } 177 ``` 178 1796. Delete the reminder as required. 180 181 ```js 182 try { 183 // The reminder ID is obtained from the callback after the reminder is published. 184 reminderAgentManager.cancelReminder(reminderId).then(() => { 185 console.log('Succeeded in canceling reminder.'); 186 }).catch(err => { 187 console.error(`Failed to cancel reminder. Code: ${err.code}, message: ${err.message}`); 188 }); 189 } catch (err) { 190 console.error(`Failed to cancel reminder. Code: ${err.code}, message: ${err.message}`); 191 } 192 ``` 193 194