background-agent-scheduled-reminder-guide.md 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 开发指导

## 场景介绍

后台代理提醒主要提供后台提醒发布接口,开发者在应用开发时,可以调用这些接口去创建定时提醒,包括倒计时、日历、闹钟三种提醒类型。使用后台代理提醒能力后,应用可以被冻结或退出,计时和弹出提醒的功能将被后台系统服务代理。


## 接口说明

reminderAgent:封装了发布、取消提醒类通知的方法

**表1** reminderAgent主要接口

| 接口名 | 描述 |
| -------- | -------- |
| function&nbsp;publishReminder(reminderReq:&nbsp;ReminderRequest,&nbsp;callback:&nbsp;AsyncCallback&lt;number&gt;):&nbsp;void;<br/>function&nbsp;publishReminder(reminderReq:&nbsp;ReminderRequest):&nbsp;Promise&lt;number&gt;; | 发布一个定时提醒类通知。<br/>单个应用有效的提醒个数最多支持30个(不包括已经超时,即后续不会再提醒的提醒实例)<br/>整个系统有效的提醒个数最多支持2000个(不包括已经超时,即后续不会再提醒的提醒实例) |
| function&nbsp;cancelReminder(reminderId:&nbsp;number,&nbsp;callback:&nbsp;AsyncCallback&lt;void&gt;):&nbsp;void;<br/>function&nbsp;cancelReminder(reminderId:&nbsp;number):&nbsp;Promise&lt;void&gt;; | 取消一个指定的提醒类通知。(reminderId从publishReminder的返回值获取) |
| function&nbsp;getValidReminders(callback:&nbsp;AsyncCallback&lt;Array&lt;ReminderRequest&gt;&gt;):&nbsp;void;<br/>function&nbsp;getValidReminders():&nbsp;Promise&lt;Array&lt;ReminderRequest&gt;&gt;; | 获取当前应用设置的所有有效的提醒。 |
| function&nbsp;cancelAllReminders(callback:&nbsp;AsyncCallback&lt;void&gt;):&nbsp;void;<br/>function&nbsp;cancelAllReminders():&nbsp;Promise&lt;void&gt;; | 取消当前应用设置的所有提醒 |
20 21
| function&nbsp;addNotificationSlot(slot:&nbsp;NotificationSlot,&nbsp;callback:&nbsp;AsyncCallback&lt;void&gt;):&nbsp;void;<br/>function&nbsp;addNotificationSlot(slot:&nbsp;NotificationSlot):&nbsp;Promise&lt;void&gt;; | 注册一个提醒类需要使用的NotificationSlot |
| function&nbsp;removeNotificationSlot(slotType:&nbsp;notification.SlotType,&nbsp;callback:&nbsp;AsyncCallback&lt;void&gt;):&nbsp;void;<br/>function&nbsp;removeNotificationSlot(slotType:&nbsp;notification.SlotType):&nbsp;Promise&lt;void&gt;; | 删除指定类型的NotificationSlot |
22 23 24 25 26 27 28

enum ActionButtonType: 在提醒弹出的通知界面上的按钮的类型。

**表2** ActionButtonType 枚举类型

| 枚举名 | 描述 |
| -------- | -------- |
廖康康 已提交
29 30
| ACTION_BUTTON_TYPE_CLOSE | 关闭按钮。关闭提醒的通知,取消延迟提醒。如果正在响铃,点击后会关闭当前提醒的铃声。 |
| ACTION_BUTTON_TYPE_SNOOZE | 延迟按钮。点击当前的提醒会延迟相应时间。 |
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

enum ReminderType: 提醒类型

**表3** ReminderType 提醒类型枚举

| 枚举名 | 描述 |
| -------- | -------- |
| REMINDER_TYPE_TIMER | 指明是倒计时类型 |
| REMINDER_TYPE_CALENDAR | 指明是日历类型 |
| REMINDER_TYPE_ALARM | 指明是闹钟类型 |

interface ActionButton:在提醒弹出的通知界面上的按钮实例

**表4** ActionButton

| 参数名 | 类型 | 必填 | 描述 |
| -------- | -------- | -------- | -------- |
| title | string | 是 | 按钮上显示的名称 |
49
| type | ActionButtonType | 是 | 按钮的类型 |
50 51 52 53 54 55 56 57 58 59

interface WantAgent: 设置点击通知后需要跳转的目标ability信息

**表5** WantAgent

| 参数名 | 类型 | 是否必选 | 描述 |
| -------- | -------- | -------- | -------- |
| pkgName | string | 是 | 目标包的名称 |
| abilityName | string | 是 | 目标ability的名称 |

廖康康 已提交
60
interface MaxScreenWantAgent: 提醒到达时自动拉起的目标界面信息,如果设备在使用中,则只弹出通知横幅框(现版本暂不支持此功能)
61 62 63 64 65 66 67 68 69 70 71 72 73 74

**表6** MaxScreenWantAgent

| 参数名 | 类型 | 必填 | 描述 |
| -------- | -------- | -------- | -------- |
| pkgName | string | 是 | 目标包的名称 |
| abilityName | string | 是 | 目标ability的名称 |

interface ReminderRequest: 需要发布的提醒实例的信息

**表7** ReminderRequest

| 参数名 | 类型 | 必填 | 描述 |
| -------- | -------- | -------- | -------- |
75 76 77 78
| reminderType | ReminderType | 是 | 提醒的类型 |
| actionButton | [ActionButton?,ActionButton?] | 否 | 弹出的提醒通知栏中显示的按钮 |
| wantAgent | WantAgent | 否 | 点击通知后需要跳转的目标ability信息 |
| maxScreenWantAgent | MaxScreenWantAgent | 否 | 提醒到达时跳转的目标包。如果设备正在使用中,则弹出一个通知框 |
79 80 81 82 83 84 85
| ringDuration | number | 否 | 响铃时长 |
| snoozeTimes | number | 否 | 延迟提醒次数 |
| timeInterval | number | 否 | 延迟提醒间隔 |
| title | string | 否 | 提醒的标题 |
| content | string | 否 | 提醒的内容 |
| expiredContent | string | 否 | 提醒“过期”时显示的扩展内容 |
| snoozeContent | string | 否 | 提醒“再响”时显示的扩展内容 |
R
rcy-hw 已提交
86
| notificationId | number | 否 | 提醒使用的notificationRequest的id,参见NotificationRequest::SetNotificationId(int32_t&nbsp;id) |
87
| slotType | SlotType | 否 | 提醒使用的slot类型 |
88 89 90

interface ReminderRequestCalendar extends ReminderRequest: 日历类提醒实例。

R
rcy-hw 已提交
91
如果没有指定重复的月或天,那么第一次指定的目标时间必须大于当前时间,否则应用将异常退出。
92 93 94 95 96

**表8** ReminderRequestCalendar

| 参数名 | 类型 | 必填 | 描述 |
| -------- | -------- | -------- | -------- |
R
rcy-hw 已提交
97
| dateTime | LocalDateTime | 是 | 设置目标时间(精确到分钟级别) |
R
rcy-hw 已提交
98 99
| repeatMonths | Array&lt;number&gt; | 否 | 设置重复提醒的月份,范围从 1 到 12 |
| repeatDays | Array&lt;number&gt; | 否 | 设置重复提醒的日期,范围从 1 到 31 |
100 101 102 103 104 105 106

interface ReminderRequestAlarm extends ReminderRequest: 闹钟类提醒实例。

**表9** ReminderRequestAlarm

| 参数名 | 类型 | 必填 | 描述 |
| -------- | -------- | -------- | -------- |
R
rcy-hw 已提交
107 108 109
| hour | number | 是 | 设置目标时间(小时),范围从 0 到 23 |
| minute | number | 是 | 设置目标时间(分钟),范围从 0 到 59 |
| daysOfWeek | Array&lt;number&gt; | 否 | 设置每个星期哪一天重复提醒,范围从 1 到 7 |
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

interface ReminderRequestTimer extends ReminderRequest:倒计时提醒实例

**表10** ReminderRequestTimer

| 参数名 | 类型 | 必填 | 描述 |
| -------- | -------- | -------- | -------- |
| triggerTimeInSeconds | number | 是 | 设置倒计时秒数 |

interface LocalDateTime:时间信息实例

**表11** LocalDateTime

| 参数名 | 类型 | 必填 | 描述 |
| -------- | -------- | -------- | -------- |
| year | number | 是 | 年 |
| month | number | 是 | 月 |
| day | number | 是 | 日 |
| hour | number | 是 | 时 |
| minute | number | 是 | 分 |
| second | number | 否 | 秒 |


## 开发步骤

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 应用需要配置权限:ohos.permission.PUBLISH_AGENT_REMINDER

发布一个10秒倒计时提醒

1. 定义一个倒计时实例
   ```
   import reminderAgent from '@ohos.reminderAgent';
R
rcy-hw 已提交
143 144 145 146 147 148
   import notification from '@ohos.notification';
   export default {
       // JS工程写法:
       // timer: {
       // eTS工程写法:
       let timer : reminderAgent.ReminderRequestTimer = {
149 150 151 152 153 154 155 156 157
           reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER,
           triggerTimeInSeconds: 10,
           actionButton: [
               {
                   title: "close",
                   type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
               }
           ],
           wantAgent: {
R
RayShih 已提交
158 159
               pkgName: "com.example.device",
               abilityName: "com.example.device.MainAbility"
160 161
           },
           maxScreenWantAgent: {
R
RayShih 已提交
162 163
               pkgName: "com.example.device",
               abilityName: "com.example.device.MainAbility"
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
           },
           title: "this is title",
           content: "this is content",
           expiredContent: "this reminder has expired",
           notificationId: 100,
           slotType: notification.SlotType.SOCIAL_COMMUNICATION
       }
   }
   ```

2. 发布提醒
   ```
   startTimer() {
       reminderAgent.publishReminder(this.timer, (err, reminderId) =>{
           this.printInfo(JSON.stringify(err));
           this.printInfo("reminderId:" + reminderId);
       });
   }
   ```

184
   HML页面:
185 186 187 188 189 190 191 192 193
   ```
   <div class="container">
       <button type="text" value="publishReminder" onclick="startTimer"></button>
   </div>
   ```

日历实例定义:

```
R
rcy-hw 已提交
194 195 196 197
// JS工程写法:
// calendar: {
// eTS工程写法:
let calendar : reminderAgent.ReminderRequestCalendar = {
198 199 200 201 202 203 204 205 206
    reminderType: reminderAgent.ReminderType.REMINDER_TYPE_CALENDAR,
    dateTime: {
        year: 2050,
        month: 7,
        day: 30,
        hour: 11,
        minute: 14,
        second: 30
    },
R
rcy-hw 已提交
207 208
    repeatMonths: [1],
    repeatDays: [1],
209 210 211 212 213 214 215 216 217 218 219
    actionButton: [
        {
            title: "close",
            type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
        },
        {
            title: "snooze",
            type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
        },
    ],
    wantAgent: {
R
RayShih 已提交
220 221
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
222 223
    },
    maxScreenWantAgent: {
R
RayShih 已提交
224 225
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    },
    ringDuration: 5,
    snoozeTimes: 2,
    timeInterval: 5,
    title: "this is title",
    content: "this is content",
    expiredContent: "this reminder has expired",
    snoozeContent: "remind later",
    notificationId: 100,
    slotType: notification.SlotType.SOCIAL_COMMUNICATION
}
```

闹钟实例定义:

```
R
rcy-hw 已提交
242 243 244 245
// JS工程写法:
// alarm: {
// eTS工程写法:
let alarm : reminderAgent.ReminderRequestAlarm = {
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    reminderType: reminderAgent.ReminderType.REMINDER_TYPE_ALARM,
    hour: 11,
    minute: 14,
    daysOfWeek: [0],
    actionButton: [
        {
            title: "close",
            type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_CLOSE
        },
        {
            title: "snooze",
            type: reminderAgent.ActionButtonType.ACTION_BUTTON_TYPE_SNOOZE
        },
    ],
    wantAgent: {
R
RayShih 已提交
261 262
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
263 264
    },
    maxScreenWantAgent: {
R
RayShih 已提交
265 266
        pkgName: "com.example.device",
        abilityName: "com.example.device.MainAbility"
267 268 269 270 271 272 273 274 275 276 277 278
    },
    ringDuration: 5,
    snoozeTimes: 2,
    timeInterval: 5,
    title: "this is title",
    content: "this is content",
    expiredContent: "this reminder has expired",
    snoozeContent: "remind later",
    notificationId: 100,
    slotType: notification.SlotType.SOCIAL_COMMUNICATION
}
```
279 280 281 282 283 284 285 286


## 相关实例

基于后台代理提醒开发,有以下相关实例可供参考:

- [`AlarmClock`:后台代理提醒(eTS)(API8)](https://gitee.com/openharmony/app_samples/tree/master/Notification/AlarmClock)

287
- [`FlipClock`:翻页时钟(eTS)(API8)](https://gitee.com/openharmony/app_samples/tree/master/CompleteApps/FlipClock)
288 289

- [闹钟应用(eTS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/CommonEventAndNotification/AlarmClock)