1# Agent-Powered Reminder Development 2 3 4## Available APIs 5 6The agent-powered reminder feature provides APIs for publishing background reminders. You can call these APIs to create scheduled reminders for countdown timers, calendar events, and alarm clocks. The APIs are encapsulated in the [reminderAgentManager](../reference/apis/js-apis-reminderAgentManager.md) class. 7 8 **Table 1** Major APIs in reminderAgentManager 9 10| API | Description | 11| ---------------------------------------- | ---------------------------------------- | 12| publishReminder(reminderReq: ReminderRequest, callback: AsyncCallback<number>): void<br>publishReminder(reminderReq: ReminderRequest): Promise<number> | Publishes a scheduled reminder.<br>The maximum number of valid notifications (excluding expired ones that will not pop up again) is 30 for one application<br>and 2000 for the entire system.| 13| cancelReminder(reminderId: number, callback: AsyncCallback<void>): void<br>cancelReminder(reminderId: number): Promise<void> | Cancels a specified reminder. (The value of **reminderId** is obtained from the return value of **publishReminder**.)| 14| getValidReminders(callback: AsyncCallback<Array<ReminderRequest>>): void<br>getValidReminders(): Promise<Array<ReminderRequest>> | Obtains all valid reminders set by the current application. | 15| cancelAllReminders(callback: AsyncCallback<void>): void<br>cancelAllReminders(): Promise<void> | Cancels all reminders set by the current application. | 16| addNotificationSlot(slot: NotificationSlot, callback: AsyncCallback<void>): void<br>addNotificationSlot(slot: NotificationSlot): Promise<void> | Registers a **NotificationSlot** instance to be used by the reminder. | 17| removeNotificationSlot(slotType: notification.SlotType, callback: AsyncCallback<void>): void<br>removeNotificationSlot(slotType: notification.SlotType): Promise<void> | Removes a **NotificationSlot** instance of a specified type. | 18 19 20## How to Develop 21 221. Request the **ohos.permission.PUBLISH_AGENT_REMINDER** permission. For details, see [Permission Application Guide](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file). 23 242. [Enable the notification feature](../notification/notification-enable.md). 25 263. Import the modules. 27 28 ```js 29 import reminderAgentManager from '@ohos.reminderAgentManager'; 30 import NotificationManager from '@ohos.notificationManager'; 31 ``` 32 334. Define a reminder agent. You can define the following types of reminder agents based on project requirements. 34 - Countdown timer 35 36 ```js 37 let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = { 38 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER, // The reminder type is countdown timer. 39 triggerTimeInSeconds: 10, 40 actionButton: [ // Set the button type and title displayed in the reminder notification. The Close and Snooze types are supported, and the Snooze type must be used together with the snoozeTimes and timeInterval parameters. 41 { 42 title: 'close', 43 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 44 } 45 ], 46 wantAgent: {// Information about the target UIAbility that is displayed after the reminder notification is touched. 47 pkgName: 'com.example.myapplication', 48 abilityName: 'EntryAbility' 49 }, 50 maxScreenWantAgent: {// Information about the target ability that is automatically started when the specified reminder time arrives is displayed in full screen. 51 pkgName: 'com.example.myapplication', 52 abilityName: 'EntryAbility' 53 }, 54 title: 'this is title', // Reminder title. 55 title: 'this is title', // Reminder content. 56 expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 57 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. 58 slotType: NotificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 59 } 60 ``` 61 - Calendar event 62 63 ```js 64 let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = { 65 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // The reminder type is calendar event. 66 dateTime: { // Reminder time. 67 year: 2023, 68 month: 7, 69 day: 30, 70 hour: 11, 71 minute: 14, 72 second: 30 73 }, 74 repeatMonths: [1], // Month in which the reminder repeats. 75 repeatDays: [1], // Date on which the reminder repeats. 76 actionButton: [ // Set the button type and title displayed in the reminder notification. The Close and Snooze types are supported, and the Snooze type must be used together with the snoozeTimes and timeInterval parameters. 77 { 78 title: 'close', 79 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 80 }, 81 { 82 title: 'snooze', 83 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE 84 }, 85 ], 86 wantAgent: {// Information about the target UIAbility that is displayed after the reminder notification is touched. 87 pkgName: 'com.example.myapplication', 88 abilityName: 'EntryAbility' 89 }, 90 maxScreenWantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched. 91 pkgName: 'com.example.myapplication', 92 abilityName: 'EntryAbility' 93 }, 94 ringDuration: 5, // Ringing duration, in seconds. 95 snoozeTimes: 2, // Number of reminder snooze times. 96 timeInterval: 5, // Reminder snooze interval, in seconds. 97 title: 'this is title', // Reminder title. 98 content:'this is content', // Reminder content. 99 expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 100 snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed. 101 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. 102 slotType: NotificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 103 } 104 ``` 105 - Alarm clock 106 107 ```js 108 let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = { 109 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // The reminder type is alarm clock. 110 hour: 23, // Hour portion of the reminder time. 111 minute: 9, // Minute portion of the reminder time. 112 daysOfWeek: [2], // Days of a week when the reminder repeats.. 113 actionButton: [ // Set the button type and title displayed in the reminder notification. The Close and Snooze types are supported, and the Snooze type must be used together with the snoozeTimes and timeInterval parameters. 114 { 115 title: 'close', 116 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 117 }, 118 { 119 title: 'snooze', 120 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE 121 }, 122 ], 123 wantAgent: {// Information about the target UIAbility that is displayed after the reminder notification is touched. 124 pkgName: 'com.example.myapplication', 125 abilityName: 'EntryAbility' 126 }, 127 maxScreenWantAgent: { // Information about the target UIAbility that is displayed after the reminder notification is touched. 128 pkgName: 'com.example.myapplication', 129 abilityName: 'EntryAbility' 130 }, 131 ringDuration: 5, // Ringing duration, in seconds. 132 snoozeTimes: 2, // Number of reminder snooze times. 133 timeInterval: 5, // Reminder snooze interval, in seconds. 134 title: 'this is title', // Reminder title. 135 content: 'this is content', // Reminder content. 136 expiredContent: 'this reminder has expired', // Content to be displayed after the reminder expires. 137 snoozeContent: 'remind later', // Content to be displayed when the reminder is snoozed. 138 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. 139 slotType: NotificationManager.SlotType.SOCIAL_COMMUNICATION // Type of the slot used by the reminder. 140 } 141 ``` 142 1435. Publish the reminder agent. After the agent is published, your application can use the agent-powered reminder feature. 144 145 ```js 146 try { 147 reminderAgentManager.publishReminder(targetReminderAgent).then(res => { 148 console.info('publishReminder promise reminderId: ' + res); 149 let reminderId: number = res; 150 // ... 151 }).catch(err => { 152 console.info('publishReminder err code: ' + err.code + ' message:' + err.message); 153 }) 154 } catch (error) { 155 console.info('publishReminder code: ' + error.code + ' message:' + error.message); 156 } 157 ``` 158 159 The following figure shows the running effect of an alarm clock. 160 161 ![en-us_image_0000001416585578](figures/en-us_image_0000001416585578.png) 162 1636. To delete a reminder task, call [reminderAgentManager.cancelReminder()](../reference/apis/js-apis-reminderAgentManager.md#reminderagentmanagercancelreminder). 164 165 ```js 166 let reminderId = 0; // The reminderId is obtained from the callback after the agent is published. 167 168 try { 169 reminderAgentManager.cancelReminder(reminderId).then(() => { 170 console.log("cancelReminder promise"); 171 }).catch(err => { 172 console.log("promise err code: " + err.code + ", message:" + err.message); 173 }); 174 } catch (error) { 175 console.log("cancelReminder code: " + error.code + ", message: " + error.message); 176 }; 177 ``` 178