1# 代理提醒(ArkTS) 2 3## 概述 4 5### 功能介绍 6 7应用退到后台或进程终止后,仍然有一些提醒用户的定时类任务,例如购物类应用抢购提醒等,为满足此类功能场景,系统提供了代理提醒(reminderAgentManager)的能力。当应用退至后台或进程终止后,系统会代理应用做相应的提醒。当前支持的提醒类型包括:倒计时、日历和闹钟。<!--RP1--><!--RP1End--> 8 9- 倒计时类:基于倒计时的提醒功能。 10 11- 日历类:基于日历的提醒功能。 12 13- 闹钟类:基于时钟的提醒功能。 14 15### 约束与限制 16 17- **个数限制**:一个三方应用支持最多30个有效提醒<!--Del-->,一个系统应用支持最多10000个有效提醒,整个系统最多支持12000个有效提醒<!--DelEnd-->。 18 19> **说明:** 20> 21> 当到达设置的提醒时间点时,通知中心会弹出相应提醒。若未点击提醒上的关闭/CLOSE按钮,则代理提醒是有效/未过期的;若点击了关闭/CLOSE按钮,则代理提醒过期。 22> 23> 当代理提醒是周期性提醒时,如设置每天提醒,无论是否点击关闭/CLOSE按钮,代理提醒都是有效的。 24 25- **跳转限制**:点击提醒通知后跳转的应用必须是申请代理提醒的本应用。 26 27<!--RP2--><!--RP2End--> 28 29 30## 接口说明 31 32**表1** 主要接口 33 34以下是代理提醒的相关接口,下表均以Promise形式为例,更多接口及使用方式请见[后台代理提醒](../reference/apis-backgroundtasks-kit/js-apis-reminderAgentManager.md)文档。 35| 接口名 | 描述 | 36| -------- | -------- | 37| publishReminder(reminderReq: ReminderRequest): Promise<number> | 发布一个定时提醒类通知。 | 38| cancelReminder(reminderId: number): Promise<void> | 取消一个指定的提醒类通知。 | 39| getValidReminders(): Promise<Array<ReminderRequest>> | 获取当前应用设置的所有有效的提醒。 | 40| cancelAllReminders(): Promise<void> | 取消当前应用设置的所有提醒。 | 41| addNotificationSlot(slot: NotificationSlot): Promise<void> | 注册一个提醒类需要使用的通知通道(NotificationSlot)。 | 42| removeNotificationSlot(slotType: notification.SlotType): Promise<void> | 删除指定的通知通道(NotificationSlot)。 | 43 44 45## 开发步骤 46 471. 申请ohos.permission.PUBLISH_AGENT_REMINDER权限,配置方式请参阅[声明权限](../security/AccessToken/declare-permissions.md)。 48 492. [请求通知授权](../notification/notification-enable.md)。获得用户授权后,才能使用代理提醒功能。 50 513. 导入模块。 52 53 ```ts 54 import { reminderAgentManager } from '@kit.BackgroundTasksKit'; 55 import { notificationManager } from '@kit.NotificationKit'; 56 import { BusinessError } from '@kit.BasicServicesKit'; 57 ``` 58 594. 定义目标提醒代理。开发者根据实际需要,选择定义如下类型的提醒。 60 61 - 定义倒计时实例。 62 63 ```ts 64 let targetReminderAgent: reminderAgentManager.ReminderRequestTimer = { 65 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_TIMER, // 提醒类型为倒计时类型 66 triggerTimeInSeconds: 10, 67 actionButton: [ // 设置弹出的提醒通知信息上显示的按钮类型和标题 68 { 69 title: 'close', 70 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 71 } 72 ], 73 wantAgent: { // 点击提醒通知后跳转的目标UIAbility信息 74 pkgName: 'com.example.myapplication', 75 abilityName: 'EntryAbility' 76 }, 77 title: 'this is title', // 指明提醒标题 78 content: 'this is content', // 指明提醒内容 79 expiredContent: 'this reminder has expired', // 指明提醒过期后需要显示的内容 80 notificationId: 100, // 指明提醒使用的通知的ID号,相同ID号的提醒会覆盖 81 slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot类型 82 } 83 ``` 84 85 - 定义日历实例。 86 87 ```ts 88 let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = { 89 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // 提醒类型为日历类型 90 dateTime: { // 指明提醒的目标时间 91 year: 2023, 92 month: 1, 93 day: 1, 94 hour: 11, 95 minute: 14, 96 second: 30 97 }, 98 repeatMonths: [1], // 指明重复提醒的月份 99 repeatDays: [1], // 指明重复提醒的日期 100 actionButton: [ // 设置弹出的提醒通知信息上显示的按钮类型和标题 101 { 102 title: 'close', 103 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE 104 }, 105 { 106 title: 'snooze', 107 type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE 108 }, 109 ], 110 wantAgent: { // 点击提醒通知后跳转的目标UIAbility信息 111 pkgName: 'com.example.myapplication', 112 abilityName: 'EntryAbility' 113 }, 114 ringDuration: 5, // 指明响铃时长(单位:秒) 115 snoozeTimes: 2, // 指明延迟提醒次数 116 timeInterval: 5*60, // 执行延迟提醒间隔(单位:秒) 117 title: 'this is title', // 指明提醒标题 118 content: 'this is content', // 指明提醒内容 119 expiredContent: 'this reminder has expired', // 指明提醒过期后需要显示的内容 120 snoozeContent: 'remind later', // 指明延迟提醒时需要显示的内容 121 notificationId: 100, // 指明提醒使用的通知的ID号,相同ID号的提醒会覆盖 122 slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot类型 123 } 124 ``` 125 126 - 定义闹钟实例。 127 128 ```ts 129 let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = { 130 reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // 提醒类型为闹钟类型 131 hour: 23, // 指明提醒的目标时刻 132 minute: 9, // 指明提醒的目标分钟 133 daysOfWeek: [2], // 指明每周哪几天需要重复提醒 134 actionButton: [ // 设置弹出的提醒通知信息上显示的按钮类型和标题 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: { // 点击提醒通知后跳转的目标UIAbility信息 145 pkgName: 'com.example.myapplication', 146 abilityName: 'EntryAbility' 147 }, 148 ringDuration: 5, // 指明响铃时长(单位:秒) 149 snoozeTimes: 2, // 指明延迟提醒次数 150 timeInterval: 5*60, // 执行延迟提醒间隔(单位:秒) 151 title: 'this is title', // 指明提醒标题 152 content: 'this is content', // 指明提醒内容 153 expiredContent: 'this reminder has expired', // 指明提醒过期后需要显示的内容 154 snoozeContent: 'remind later', // 指明延迟提醒时需要显示的内容 155 notificationId: 99, // 指明提醒使用的通知的ID号,相同ID号的提醒会覆盖 156 slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot类型 157 } 158 ``` 159 1605. 发布相应的提醒代理。代理发布后,应用即可使用后台代理提醒功能。 161 162 ```ts 163 reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => { 164 console.info('Succeeded in publishing reminder. '); 165 let reminderId: number = res; // 发布的提醒ID 166 }).catch((err: BusinessError) => { 167 console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`); 168 }) 169 ``` 170 1716. 根据需要删除提醒任务。 172 173 ```ts 174 let reminderId: number = 1; 175 // reminderId的值从发布提醒代理成功之后的回调中获得 176 reminderAgentManager.cancelReminder(reminderId).then(() => { 177 console.log('Succeeded in canceling reminder.'); 178 }).catch((err: BusinessError) => { 179 console.error(`Failed to cancel reminder. Code: ${err.code}, message: ${err.message}`); 180 }); 181 ``` 182 183## 相关实例 184 185基于代理提醒,有以下相关实例可供参考: 186 187- [后台代理提醒(ArkTS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/TaskManagement/ReminderAgentManager) 188 189- [翻页时钟(ArkTS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/Tools/FlipClock)