js-apis-reminderAgent.md 17.9 KB
Newer Older
E
ester.zhou 已提交
1
# Reminder Agent
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5 6


E
ester.zhou 已提交
7
## Modules to Import
Z
zengyawen 已提交
8 9

```
E
ester.zhou 已提交
10
import reminderAgent from'@ohos.reminderAgent';
Z
zengyawen 已提交
11 12
```

E
ester.zhou 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

## Required Permissions

ohos.permission.PUBLISH_AGENT_REMINDER


## reminderAgent.publishReminder

publishReminder(reminderReq: ReminderRequest, callback: AsyncCallback<number>): void

Publishes an agent-powered reminder. This API uses an asynchronous callback to return the result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | reminderReq | [ReminderRequest](#reminderrequest) | Yes| Reminder to be published.|
  | callback | AsyncCallback<number> | Yes| Asynchronous callback used to return the published reminder's ID.|

- Example
  ```
  export default {    data: {timer: {
              reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER,
              triggerTimeInSeconds: 3
          }
      },
      startTimer() {
          reminderAgent.publishReminder(timer, (err, reminderId) => {    console.log("reminderId = " + reminderId);
          });
      }
  }
  ```


## reminderAgent.publishReminder

publishReminder(reminderReq: ReminderRequest): Promise<number>

Publishes an agent-powered reminder. This API uses a promise callback to return the result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | reminderReq | [ReminderRequest](#reminderrequest) | Yes| Reminder to be published.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise<number> | Promise used to return the published reminder's ID.|

- Example
  ```
  export default {    data: {timer: {
              reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER,
              triggerTimeInSeconds: 3
          }
      },
      startTimer() {
          reminderAgent.publishReminder(this.timer).then((reminderId) => {
              console.log("reminderId = " + reminderId);
          });
      }
  }
  ```


## reminderAgent.cancelReminder

cancelReminder(reminderId: number, callback: AsyncCallback<void>): void

Cancels the reminder with the specified ID. This API uses an asynchronous callback to return the cancellation result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| reminderId | number | Yes| ID of the reminder to cancel.|
| callback | AsyncCallback<void> | Yes| Asynchronous callback used to return the result.|

- Example
Z
zengyawen 已提交
104 105 106

```
export default {
E
ester.zhou 已提交
107
    cancel() {        reminderAgent.cancelReminder(1, (err, data) => {
Z
zengyawen 已提交
108 109 110 111 112 113
            console.log("do next");
        });
    }
}
```

E
ester.zhou 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

## reminderAgent.cancelReminder

cancelReminder(reminderId: number): Promise<void>

Cancels the reminder with the specified ID. This API uses a promise to return the cancellation result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| reminderId | number | Yes| ID of the reminder to cancel.|

- Return value

| Type| Description|
| -------- | -------- |
| Promise<void> | Promise used to return the result.|

- Example
Z
zengyawen 已提交
138 139 140 141 142 143 144 145 146 147 148

```
export default {
    cancel() {
        reminderAgent.cancelReminder(1).then(() => {
            console.log("do next");
        });
    }
}
```

E
ester.zhou 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

## reminderAgent.getValidReminders

getValidReminders(callback: AsyncCallback<Array<ReminderRequest>>): void

Obtains all valid (not yet expired) reminders set by the current application. This API uses an asynchronous callback to return the reminders.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<Array<[ReminderRequest](#reminderrequest)>> | Yes| Asynchronous callback used to return an array of all valid reminders set by the current application.|

- Example
Z
zengyawen 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

```
reminderAgent.getValidReminders((err, reminders) => {
    for (let i = 0; i < reminders.length; i++) {
        console.log("getValidReminders = " + reminders[i]);
        console.log("getValidReminders, reminderType = " + reminders[i].reminderType);
        for (let j = 0; j < reminders[i].actionButton.length; j++) {
            console.log("getValidReminders, actionButton.title = " + reminders[i].actionButton[j].title);
            console.log("getValidReminders, actionButton.type = " + reminders[i].actionButton[j].type);
        }
        console.log("getValidReminders, wantAgent.pkgName = " + reminders[i].wantAgent.pkgName);
        console.log("getValidReminders, wantAgent.abilityName = " + reminders[i].wantAgent.abilityName);
        console.log("getValidReminders, maxScreenWantAgent.pkgName = " + reminders[i].maxScreenWantAgent.pkgName);
        console.log("getValidReminders, maxScreenWantAgent.abilityName = " + reminders[i].maxScreenWantAgent.abilityName);
        console.log("getValidReminders, ringDuration = " + reminders[i].ringDuration);
        console.log("getValidReminders, snoozeTimes = " + reminders[i].snoozeTimes);
        console.log("getValidReminders, timeInterval = " + reminders[i].timeInterval);
        console.log("getValidReminders, title = " + reminders[i].title);
        console.log("getValidReminders, content = " + reminders[i].content);
        console.log("getValidReminders, expiredContent = " + reminders[i].expiredContent);
        console.log("getValidReminders, snoozeContent = " + reminders[i].snoozeContent);
        console.log("getValidReminders, notificationId = " + reminders[i].notificationId);
        console.log("getValidReminders, slotType = " + reminders[i].slotType);
    }
})
```


E
ester.zhou 已提交
195 196 197 198 199
## reminderAgent.getValidReminders

getValidReminders(): Promise&lt;Array&lt;ReminderRequest&gt;&gt;

Obtains all valid (not yet expired) reminders set by the current application. This API uses a promise to return the reminders.
Z
zengyawen 已提交
200

E
ester.zhou 已提交
201
- System capability
Z
zengyawen 已提交
202

E
ester.zhou 已提交
203
  SystemCapability.Notification.ReminderAgent
Z
zengyawen 已提交
204

E
ester.zhou 已提交
205
- Return value
Z
zengyawen 已提交
206

E
ester.zhou 已提交
207 208 209 210 211
| Type| Description|
| -------- | -------- |
| Promise&lt;Array&lt;[ReminderRequest](#reminderrequest)&gt;&gt; | Promise used to return an array of all valid reminders set by the current application.|

- Example
Z
zengyawen 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

```
reminderAgent.getValidReminders().then((reminders) => {    
    for (let i = 0; i < reminders.length; i++) {
        console.log("getValidReminders = " + reminders[i]);
        console.log("getValidReminders, reminderType = " + reminders[i].reminderType);
        for (let j = 0; j < reminders[i].actionButton.length; j++) {
            console.log("getValidReminders, actionButton.title = " + reminders[i].actionButton[j].title);
            console.log("getValidReminders, actionButton.type = " + reminders[i].actionButton[j].type);
        }
        console.log("getValidReminders, wantAgent.pkgName = " + reminders[i].wantAgent.pkgName);
        console.log("getValidReminders, wantAgent.abilityName = " + reminders[i].wantAgent.abilityName);
        console.log("getValidReminders, maxScreenWantAgent.pkgName = " + reminders[i].maxScreenWantAgent.pkgName);
        console.log("getValidReminders, maxScreenWantAgent.abilityName = " + reminders[i].maxScreenWantAgent.abilityName);
        console.log("getValidReminders, ringDuration = " + reminders[i].ringDuration);
        console.log("getValidReminders, snoozeTimes = " + reminders[i].snoozeTimes);
        console.log("getValidReminders, timeInterval = " + reminders[i].timeInterval);
        console.log("getValidReminders, title = " + reminders[i].title);
        console.log("getValidReminders, content = " + reminders[i].content);
        console.log("getValidReminders, expiredContent = " + reminders[i].expiredContent);
        console.log("getValidReminders, snoozeContent = " + reminders[i].snoozeContent);
        console.log("getValidReminders, notificationId = " + reminders[i].notificationId);
        console.log("getValidReminders, slotType = " + reminders[i].slotType);
    }
})
```

E
ester.zhou 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

## reminderAgent.cancelAllReminders

cancelAllReminders(callback: AsyncCallback&lt;void&gt;): void

Cancels all reminders set by the current application. This API uses an asynchronous callback to return the cancellation result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Asynchronous callback used to return the result.|

- Example
Z
zengyawen 已提交
257 258 259

```
reminderAgent.cancelAllReminders((err, data) =>{ 
E
ester.zhou 已提交
260
    console.log("do next")})
Z
zengyawen 已提交
261 262 263
```


E
ester.zhou 已提交
264
## reminderAgent.cancelAllReminders
Z
zengyawen 已提交
265

E
ester.zhou 已提交
266
cancelAllReminders(): Promise&lt;void&gt;
Z
zengyawen 已提交
267

E
ester.zhou 已提交
268
Cancels all reminders set by the current application. This API uses a promise to return the cancellation result.
Z
zengyawen 已提交
269

E
ester.zhou 已提交
270
- System capability
Z
zengyawen 已提交
271

E
ester.zhou 已提交
272 273 274 275 276 277 278 279 280
  SystemCapability.Notification.ReminderAgent

- Return value

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

- Example
Z
zengyawen 已提交
281 282 283

```
reminderAgent.cancelAllReminders().then(() => {
E
ester.zhou 已提交
284
    console.log("do next")})
Z
zengyawen 已提交
285 286
```

E
ester.zhou 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

## reminderAgent.addNotificationSlot

addNotificationSlot(slot: NotificationSlot, callback: AsyncCallback&lt;void&gt;): void

Adds a reminder notification slot. This API uses an asynchronous callback to return the result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| slot | [NotificationSlot](js-apis-notification.md#notificationslot) | Yes| Reminder notification slot to add.|
| callback | AsyncCallback&lt;void&gt; | Yes| Asynchronous callback used to return the result.|

- Example
Z
zengyawen 已提交
306 307

```
E
ester.zhou 已提交
308
export default {    data: {        mySlot: {
Z
zengyawen 已提交
309 310
            type: 3,
            sound: "/sdcard/music2.mp3"
E
ester.zhou 已提交
311
        }    },
Z
zengyawen 已提交
312 313 314 315 316 317 318 319
    addSlot() {
        reminderAgent.addNotificationSlot(this.mySlot, (err, data) => {
            console.log("do next");
        });
    }
}
```

E
ester.zhou 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

## reminderAgent.addNotificationSlot

addNotificationSlot(slot: NotificationSlot): Promise&lt;void&gt;

Adds a reminder notification slot. This API uses a promise to return the result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| slot | [NotificationSlot](js-apis-notification.md#notificationslot) | Yes| Reminder notification slot to add.|

- Return value

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

- Example
Z
zengyawen 已提交
344 345

```
E
ester.zhou 已提交
346
export default {    data: {        mySlot: {
Z
zengyawen 已提交
347 348
            type: 3,
            sound: "/sdcard/music2.mp3"
E
ester.zhou 已提交
349
        }    },
Z
zengyawen 已提交
350 351
    addSlot() {
        reminderAgent.addNotificationSlot(this.mySlot).then(() => {
E
ester.zhou 已提交
352
   console.log("do next");
Z
zengyawen 已提交
353 354 355 356 357
        });
    }
}
```

E
ester.zhou 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

## reminderAgent.removeNotificationSlot

removeNotificationSlot(slotType: notification.SlotType, callback: AsyncCallback&lt;void&gt;): void

Removes a notification slot of a specified type. This API uses an asynchronous callback to return the result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| slotType | [notification.SlotType](js-apis-notification.md#slottype) | Yes| Type of the reminder notification slot to remove.|
| callback | AsyncCallback&lt;void&gt; | Yes| Asynchronous callback used to return the result.|

- Example
Z
zengyawen 已提交
377 378 379

```
export default {
E
ester.zhou 已提交
380
    removeSlot() {reminderAgent.removeNotificationSlot(notification.SlotType.CONTENT_INFORMATION, (err, data) => {
Z
zengyawen 已提交
381
            console.log("do next");
E
ester.zhou 已提交
382
});
Z
zengyawen 已提交
383 384 385 386
    }
}
```

E
ester.zhou 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

## reminderAgent.removeNotificationSlot

removeNotificationSlot(slotType: notification.SlotType): Promise&lt;void&gt;

Removes a notification slot of a specified type. This API uses a promise to return the result.

- System capability

  SystemCapability.Notification.ReminderAgent

- Parameters

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| slotType | [notification.SlotType](js-apis-notification.md#slottype) | Yes| Type of the reminder notification slot to remove.|

- Return value

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

- Example
Z
zengyawen 已提交
411 412 413

```
export default {
E
ester.zhou 已提交
414
    removeSlot() {        reminderAgent.removeNotificationSlot(notification.SlotType.CONTENT_INFORMATION).then(() => {
Z
zengyawen 已提交
415 416 417 418 419 420
            console.log("do next");
        });
    }
}
```

E
ester.zhou 已提交
421 422

## ActionButtonType
Z
zengyawen 已提交
423 424 425

Enumerates button types.

E
ester.zhou 已提交
426 427 428 429 430 431 432 433 434
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Default Value| Description|
| -------- | -------- | -------- |
| ACTION_BUTTON_TYPE_CLOSE | 0 | Button for closing the reminder.|
| ACTION_BUTTON_TYPE_SNOOZE | 1 | Button for snoozing the reminder.|


## ReminderType
Z
zengyawen 已提交
435 436 437

Enumerates reminder types.

E
ester.zhou 已提交
438 439 440 441 442 443 444 445 446 447
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Default Value| Description|
| -------- | -------- | -------- |
| REMINDER_TYPE_TIMER | 0 | Countdown reminder.|
| REMINDER_TYPE_CALENDAR | 1 | Calendar reminder.|
| REMINDER_TYPE_ALARM | 2 | Alarm reminder.|


## ActionButton
Z
zengyawen 已提交
448 449 450

Defines a button displayed in the reminder notification.

E
ester.zhou 已提交
451 452 453 454 455 456 457 458 459
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| title | string | Yes| Text on the button.|
| type | [ActionButtonType](#actionbuttontype) | Yes| Button type.|


## WantAgent
Z
zengyawen 已提交
460 461 462

Sets the package and ability that are redirected to when the reminder notification is clicked.

E
ester.zhou 已提交
463 464 465 466 467 468 469 470 471
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pkgName | string | Yes| Name of the package that is redirected to when the reminder notification is clicked.|
| abilityName | string | Yes| Name of the ability that is redirected to when the reminder notification is clicked.|


## MaxScreenWantAgent
Z
zengyawen 已提交
472 473 474

Sets the name of the target package and ability to start automatically when the reminder arrives and the device is not in use. If the device is in use, a notification will be displayed.

E
ester.zhou 已提交
475 476 477 478 479 480 481 482 483
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pkgName | string | Yes| Name of the package that is automatically started when the reminder arrives and the device is not in use.|
| abilityName | string | Yes| Name of the ability that is automatically started when the reminder arrives and the device is not in use.|


## ReminderRequest
Z
zengyawen 已提交
484 485 486

Defines the reminder to publish.

E
ester.zhou 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| reminderType | ReminderType | Yes| Type of the reminder.|
| actionButton | [ActionButton?,&nbsp;ActionButton?] | No| Action button displayed in the reminder notification. (The parameter is optional. Up to two buttons are supported.)|
| wantAgent | WantAgent | No| Information about the ability that is redirected to when the notification is clicked.|
| maxScreenWantAgent | MaxScreenWantAgent | No| Information about the ability that is automatically started when the reminder arrives. If the device is in use, a notification will be displayed.|
| ringDuration | number | No| Ringing duration.|
| snoozeTimes | number | No| Number of reminder snooze times.|
| timeInterval | number | No| Reminder snooze interval.|
| title | string | No| Reminder title.|
| content | string | No| Reminder content.|
| expiredContent | string | No| Content to be displayed after the reminder expires.|
| snoozeContent | string | No| Content to be displayed when the reminder is snoozing.|
| notificationId | number | No| Notification ID used by the reminder. If there are reminders with the same notification ID, the later one will overwrite the earlier one.|
| slotType | [notification.SlotType](js-apis-notification.md#slottype) | No| Type of the slot used by the reminder.|


## ReminderRequestCalendar
Z
zengyawen 已提交
507 508 509 510 511

ReminderRequestCalendar extends ReminderRequest

Defines a reminder for a calendar event.

E
ester.zhou 已提交
512 513 514 515 516 517 518 519 520 521
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| dateTime | [LocalDateTime](#localdatetime) | Yes| Reminder time.|
| repeatMonths | Array&lt;number&gt; | No| Month in which the reminder repeats.|
| repeatDays | Array&lt;number&gt; | No| Date on which the reminder repeats.|


## ReminderRequestAlarm
Z
zengyawen 已提交
522 523 524

ReminderRequestAlarm extends ReminderRequest

E
ester.zhou 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538
Defines a reminder for an alarm.

- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Type| Mandatory| Description:|
| -------- | -------- | -------- | -------- |
| hour | number | Yes| Hour portion of the reminder time.|
| minute | number | Yes| Minute portion of the reminder time.|
| daysOfWeek | Array&lt;number&gt; | No| Days of a week when the reminder repeats.|


## ReminderRequestTimer

ReminderRequestTimer extends ReminderRequest
Z
zengyawen 已提交
539 540 541

Defines a reminder for a scheduled timer.

E
ester.zhou 已提交
542 543 544 545 546 547 548 549
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| triggerTimeInSeconds | number | Yes| Number of seconds in the countdown timer.|


## LocalDateTime
Z
zengyawen 已提交
550 551 552

Sets the time information for a calendar reminder.

E
ester.zhou 已提交
553 554 555 556 557 558 559 560 561 562
- **System capability**: SystemCapability.Notification.ReminderAgent

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| year | number | Yes| Year.|
| month | number | Yes| Month.|
| day | number | Yes| Date.|
| hour | number | Yes| Hour.|
| minute | number | Yes| Minute.|
| second | number | No| Second.|