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&lt;number&gt; | 发布一个定时提醒类通知 |
38| cancelReminder(reminderId: number): Promise&lt;void&gt; | 取消一个指定的提醒类通知 |
39| getValidReminders(): Promise&lt;Array&lt;ReminderRequest&gt;&gt; | 获取当前应用设置的所有有效的提醒 |
40| cancelAllReminders(): Promise&lt;void&gt; | 取消当前应用设置的所有提醒 |
41| addNotificationSlot(slot: NotificationSlot): Promise&lt;void&gt; | 注册一个提醒类需要使用的通知通道(NotificationSlot) |
42| removeNotificationSlot(slotType: notification.SlotType): Promise&lt;void&gt; | 删除指定的通知通道(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        maxScreenWantAgent: { // 全屏显示提醒到达时自动拉起的目标UIAbility信息
78          pkgName: 'com.example.myapplication',
79          abilityName: 'EntryAbility'
80        },
81        title: 'this is title', // 指明提醒标题
82        content: 'this is content', // 指明提醒内容
83        expiredContent: 'this reminder has expired', // 指明提醒过期后需要显示的内容
84        notificationId: 100, // 指明提醒使用的通知的ID号,相同ID号的提醒会覆盖
85        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot类型
86      }
87      ```
88
89   - 定义日历实例。
90
91      ```ts
92      let targetReminderAgent: reminderAgentManager.ReminderRequestCalendar = {
93        reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_CALENDAR, // 提醒类型为日历类型
94        dateTime: {   // 指明提醒的目标时间
95          year: 2023,
96          month: 1,
97          day: 1,
98          hour: 11,
99          minute: 14,
100          second: 30
101        },
102        repeatMonths: [1], // 指明重复提醒的月份
103        repeatDays: [1], // 指明重复提醒的日期
104        actionButton: [ // 设置弹出的提醒通知信息上显示的按钮类型和标题
105          {
106            title: 'close',
107            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
108          },
109          {
110            title: 'snooze',
111            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
112          },
113        ],
114        wantAgent: { // 点击提醒通知后跳转的目标UIAbility信息
115          pkgName: 'com.example.myapplication',
116          abilityName: 'EntryAbility'
117        },
118        maxScreenWantAgent: { // 全屏显示提醒到达时自动拉起的目标UIAbility信息
119          pkgName: 'com.example.myapplication',
120          abilityName: 'EntryAbility'
121        },
122        ringDuration: 5, // 指明响铃时长(单位:秒)
123        snoozeTimes: 2, // 指明延迟提醒次数
124        timeInterval: 5*60, // 执行延迟提醒间隔(单位:秒)
125        title: 'this is title', // 指明提醒标题
126        content: 'this is content', // 指明提醒内容
127        expiredContent: 'this reminder has expired', // 指明提醒过期后需要显示的内容
128        snoozeContent: 'remind later', // 指明延迟提醒时需要显示的内容
129        notificationId: 100, // 指明提醒使用的通知的ID号,相同ID号的提醒会覆盖
130        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot类型
131      }
132      ```
133
134   - 定义闹钟实例。
135
136      ```ts
137      let targetReminderAgent: reminderAgentManager.ReminderRequestAlarm = {
138        reminderType: reminderAgentManager.ReminderType.REMINDER_TYPE_ALARM, // 提醒类型为闹钟类型
139        hour: 23, // 指明提醒的目标时刻
140        minute: 9, // 指明提醒的目标分钟
141        daysOfWeek: [2], // 指明每周哪几天需要重复提醒
142        actionButton: [ // 设置弹出的提醒通知信息上显示的按钮类型和标题
143          {
144            title: 'close',
145            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
146          },
147          {
148            title: 'snooze',
149            type: reminderAgentManager.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
150          },
151        ],
152        wantAgent: { // 点击提醒通知后跳转的目标UIAbility信息
153          pkgName: 'com.example.myapplication',
154          abilityName: 'EntryAbility'
155        },
156        maxScreenWantAgent: { // 全屏显示提醒到达时自动拉起的目标UIAbility信息
157          pkgName: 'com.example.myapplication',
158          abilityName: 'EntryAbility'
159        },
160        ringDuration: 5, // 指明响铃时长(单位:秒)
161        snoozeTimes: 2, // 指明延迟提醒次数
162        timeInterval: 5*60, // 执行延迟提醒间隔(单位:秒)
163        title: 'this is title', // 指明提醒标题
164        content: 'this is content', // 指明提醒内容
165        expiredContent: 'this reminder has expired', // 指明提醒过期后需要显示的内容
166        snoozeContent: 'remind later', // 指明延迟提醒时需要显示的内容
167        notificationId: 99, // 指明提醒使用的通知的ID号,相同ID号的提醒会覆盖
168        slotType: notificationManager.SlotType.SOCIAL_COMMUNICATION // 指明提醒的Slot类型
169      }
170      ```
171
1725. 发布相应的提醒代理。代理发布后,应用即可使用后台代理提醒功能。
173
174   ```ts
175    reminderAgentManager.publishReminder(targetReminderAgent).then((res: number) => {
176      console.info('Succeeded in publishing reminder. ');
177      let reminderId: number = res; // 发布的提醒ID
178    }).catch((err: BusinessError) => {
179      console.error(`Failed to publish reminder. Code: ${err.code}, message: ${err.message}`);
180    })
181   ```
182
1836. 根据需要删除提醒任务。
184
185   ```ts
186    let reminderId: number = 1;
187    // reminderId的值从发布提醒代理成功之后的回调中获得
188    reminderAgentManager.cancelReminder(reminderId).then(() => {
189      console.log('Succeeded in canceling reminder.');
190    }).catch((err: BusinessError) => {
191      console.error(`Failed to cancel reminder. Code: ${err.code}, message: ${err.message}`);
192    });
193   ```
194
195## 相关实例
196
197基于代理提醒,有以下相关实例可供参考:
198
199- [后台代理提醒(ArkTS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/TaskManagement/ReminderAgentManager)
200
201- [翻页时钟(ArkTS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/Tools/FlipClock)