1# @ohos.reminderAgent (后台代理提醒) 2 3本模块提供后台代理提醒的能力。 4 5开发应用时,开发者可以调用后台提醒发布的接口创建定时提醒,包括倒计时、日历、闹钟三种提醒类型。使用后台代理提醒能力后,应用可以被冻结或退出,计时和弹出提醒的功能将被后台系统服务代理。 6 7> **说明:** 8> 9> 从API Version 9 开始,该接口不再维护,推荐使用新接口[@ohos.reminderAgentManager (后台代理提醒)](js-apis-reminderAgentManager.md) 10> 11> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12 13 14## 导入模块 15 16```js 17import reminderAgent from'@ohos.reminderAgent'; 18``` 19 20 21## reminderAgent.publishReminder 22 23publishReminder(reminderReq: ReminderRequest, callback: AsyncCallback<number>): void 24 25发布一个后台代理提醒,使用callback方式实现异步调用,该方法需要申请通知弹窗[Notification.requestEnableNotification](js-apis-notification.md#notificationrequestenablenotification8)后才能调用。 26 27**需要权限**: ohos.permission.PUBLISH_AGENT_REMINDER 28 29**系统能力**: SystemCapability.Notification.ReminderAgent 30 31**参数**: 32 33 | 参数名 | 类型 | 必填 | 说明 | 34 | -------- | -------- | -------- | -------- | 35 | reminderReq | [ReminderRequest](#reminderrequest) | 是 | 需要发布的提醒实例。 | 36 | callback | AsyncCallback<number> | 是 | 异步回调,返回当前发布的提醒的reminderId。 | 37 38**示例**: 39```js 40 let timer = { 41 reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER, 42 triggerTimeInSeconds: 10 43 } 44 reminderAgent.publishReminder(timer, (err, reminderId) => { 45 console.log("callback, reminderId = " + reminderId); 46 }); 47``` 48 49 50## reminderAgent.publishReminder 51 52publishReminder(reminderReq: ReminderRequest): Promise<number> 53 54发布一个后台代理提醒,使用Promise方式实现异步调用,该方法需要申请通知弹窗[Notification.requestEnableNotification](js-apis-notification.md#notificationrequestenablenotification8)后才能调用。 55 56**需要权限**: ohos.permission.PUBLISH_AGENT_REMINDER 57 58**系统能力**: SystemCapability.Notification.ReminderAgent 59 60**参数**: 61 | 参数名 | 类型 | 必填 | 说明 | 62 | -------- | -------- | -------- | -------- | 63 | reminderReq | [ReminderRequest](#reminderrequest) | 是 | 需要发布的提醒实例。 | 64 65**返回值**: 66 | 类型 | 说明 | 67 | -------- | -------- | 68 | Promise<number> | 返回提醒的reminderId。 | 69 70**示例**: 71```js 72 let timer = { 73 reminderType: reminderAgent.ReminderType.REMINDER_TYPE_TIMER, 74 triggerTimeInSeconds: 10 75 } 76 reminderAgent.publishReminder(timer).then((reminderId) => { 77 console.log("promise, reminderId = " + reminderId); 78 }); 79``` 80 81 82## reminderAgent.cancelReminder 83 84cancelReminder(reminderId: number, callback: AsyncCallback<void>): void 85 86取消指定id的提醒,使用callback方式实现异步调用。 87 88**系统能力**: SystemCapability.Notification.ReminderAgent 89 90**参数**: 91 92| 参数名 | 类型 | 必填 | 说明 | 93| -------- | -------- | -------- | -------- | 94| reminderId | number | 是 | 目标reminder的id号,[publishReminder](#reminderagentpublishreminder)方法调用成功后获得。 | 95| callback | AsyncCallback<void> | 是 | 异步回调。 | 96 97**示例**: 98 99```js 100reminderAgent.cancelReminder(1, (err, data) => { 101 console.log("cancelReminder callback"); 102}); 103``` 104 105 106## reminderAgent.cancelReminder 107 108cancelReminder(reminderId: number): Promise<void> 109 110取消指定id的提醒,使用Promise方式实现异步调用。 111 112**系统能力**: SystemCapability.Notification.ReminderAgent 113 114**参数**: 115 116| 参数名 | 类型 | 必填 | 说明 | 117| -------- | -------- | -------- | -------- | 118| reminderId | number | 是 | 目标reminder的id号,[publishReminder](#reminderagentpublishreminder)方法调用成功后获得。 | 119 120**返回值**: 121 122| 类型 | 说明 | 123| -------- | -------- | 124| Promise<void> | Promise类型异步回调。 | 125 126**示例**: 127 128```js 129reminderAgent.cancelReminder(1).then(() => { 130 console.log("cancelReminder promise"); 131}); 132``` 133 134 135## reminderAgent.getValidReminders 136 137getValidReminders(callback: AsyncCallback<Array<ReminderRequest>>): void 138 139获取当前应用已设置的所有有效(未过期)的提醒,使用callback方式实现异步调用。 140 141**系统能力**: SystemCapability.Notification.ReminderAgent 142 143**参数**: 144 145| 参数名 | 类型 | 必填 | 说明 | 146| -------- | -------- | -------- | -------- | 147| callback | AsyncCallback<Array<[ReminderRequest](#reminderrequest)>> | 是 | 异步回调,返回当前应用已设置的所有有效(未过期)的提醒。 | 148 149**示例**: 150 151```js 152reminderAgent.getValidReminders((err, reminders) => { 153 console.log("callback, getValidReminders length = " + reminders.length); 154 for (let i = 0; i < reminders.length; i++) { 155 console.log("getValidReminders = " + reminders[i]); 156 console.log("getValidReminders, reminderType = " + reminders[i].reminderType); 157 for (let j = 0; j < reminders[i].actionButton.length; j++) { 158 console.log("getValidReminders, actionButton.title = " + reminders[i].actionButton[j].title); 159 console.log("getValidReminders, actionButton.type = " + reminders[i].actionButton[j].type); 160 } 161 console.log("getValidReminders, wantAgent.pkgName = " + reminders[i].wantAgent.pkgName); 162 console.log("getValidReminders, wantAgent.abilityName = " + reminders[i].wantAgent.abilityName); 163 console.log("getValidReminders, maxScreenWantAgent.pkgName = " + reminders[i].maxScreenWantAgent.pkgName); 164 console.log("getValidReminders, maxScreenWantAgent.abilityName = " + reminders[i].maxScreenWantAgent.abilityName); 165 console.log("getValidReminders, ringDuration = " + reminders[i].ringDuration); 166 console.log("getValidReminders, snoozeTimes = " + reminders[i].snoozeTimes); 167 console.log("getValidReminders, timeInterval = " + reminders[i].timeInterval); 168 console.log("getValidReminders, title = " + reminders[i].title); 169 console.log("getValidReminders, content = " + reminders[i].content); 170 console.log("getValidReminders, expiredContent = " + reminders[i].expiredContent); 171 console.log("getValidReminders, snoozeContent = " + reminders[i].snoozeContent); 172 console.log("getValidReminders, notificationId = " + reminders[i].notificationId); 173 console.log("getValidReminders, slotType = " + reminders[i].slotType); 174 } 175}) 176``` 177 178 179## reminderAgent.getValidReminders 180 181getValidReminders(): Promise<Array<ReminderRequest>> 182 183获取当前应用已设置的所有有效(未过期)的提醒,使用Promise方式实现异步调用。 184 185**系统能力**: SystemCapability.Notification.ReminderAgent 186 187**返回值**: 188 189| 类型 | 说明 | 190| -------- | -------- | 191| Promise<Array<[ReminderRequest](#reminderrequest)>> | 返回当前应用已设置的所有有效(未过期)的提醒。 | 192 193**示例**: 194 195```js 196reminderAgent.getValidReminders().then((reminders) => { 197 console.log("promise, getValidReminders length = " + reminders.length); 198 for (let i = 0; i < reminders.length; i++) { 199 console.log("getValidReminders = " + reminders[i]); 200 console.log("getValidReminders, reminderType = " + reminders[i].reminderType); 201 for (let j = 0; j < reminders[i].actionButton.length; j++) { 202 console.log("getValidReminders, actionButton.title = " + reminders[i].actionButton[j].title); 203 console.log("getValidReminders, actionButton.type = " + reminders[i].actionButton[j].type); 204 } 205 console.log("getValidReminders, wantAgent.pkgName = " + reminders[i].wantAgent.pkgName); 206 console.log("getValidReminders, wantAgent.abilityName = " + reminders[i].wantAgent.abilityName); 207 console.log("getValidReminders, maxScreenWantAgent.pkgName = " + reminders[i].maxScreenWantAgent.pkgName); 208 console.log("getValidReminders, maxScreenWantAgent.abilityName = " + reminders[i].maxScreenWantAgent.abilityName); 209 console.log("getValidReminders, ringDuration = " + reminders[i].ringDuration); 210 console.log("getValidReminders, snoozeTimes = " + reminders[i].snoozeTimes); 211 console.log("getValidReminders, timeInterval = " + reminders[i].timeInterval); 212 console.log("getValidReminders, title = " + reminders[i].title); 213 console.log("getValidReminders, content = " + reminders[i].content); 214 console.log("getValidReminders, expiredContent = " + reminders[i].expiredContent); 215 console.log("getValidReminders, snoozeContent = " + reminders[i].snoozeContent); 216 console.log("getValidReminders, notificationId = " + reminders[i].notificationId); 217 console.log("getValidReminders, slotType = " + reminders[i].slotType); 218 } 219}) 220``` 221 222 223## reminderAgent.cancelAllReminders 224 225cancelAllReminders(callback: AsyncCallback<void>): void 226 227取消当前应用所有的提醒,使用callback方式实现异步调用。 228 229**系统能力**: SystemCapability.Notification.ReminderAgent 230 231**参数**: 232 233| 参数名 | 类型 | 必填 | 说明 | 234| -------- | -------- | -------- | -------- | 235| callback | AsyncCallback<void> | 是 | 异步回调。 | 236 237**示例**: 238 239```js 240reminderAgent.cancelAllReminders((err, data) =>{ 241 console.log("cancelAllReminders callback") 242}) 243``` 244 245 246## reminderAgent.cancelAllReminders 247 248cancelAllReminders(): Promise<void> 249 250取消当前应用所有的提醒,使用Promise方式实现异步调用。 251 252**系统能力**: SystemCapability.Notification.ReminderAgent 253 254**返回值**: 255 256| 类型 | 说明 | 257| -------- | -------- | 258| Promise<void> | Promise类型异步回调。 | 259 260**示例**: 261 262```js 263reminderAgent.cancelAllReminders().then(() => { 264 console.log("cancelAllReminders promise") 265}) 266``` 267 268 269## reminderAgent.addNotificationSlot 270 271addNotificationSlot(slot: NotificationSlot, callback: AsyncCallback<void>): void 272 273添加一个NotificationSlot,使用callback方式实现异步调用。 274 275**系统能力**: SystemCapability.Notification.ReminderAgent 276 277**参数**: 278 279| 参数名 | 类型 | 必填 | 说明 | 280| -------- | -------- | -------- | -------- | 281| slot | [NotificationSlot](js-apis-notification.md#notificationslot) | 是 | notification slot实例,仅支持设置其type属性。 | 282| callback | AsyncCallback<void> | 是 | 异步回调。 | 283 284**示例**: 285 286```js 287import notification from '@ohos.notification' 288 289let mySlot = { 290 type: notification.SlotType.SOCIAL_COMMUNICATION 291} 292reminderAgent.addNotificationSlot(mySlot, (err, data) => { 293 console.log("addNotificationSlot callback"); 294}); 295``` 296 297 298## reminderAgent.addNotificationSlot 299 300addNotificationSlot(slot: NotificationSlot): Promise<void> 301 302添加一个NotificationSlot,使用Promise方式实现异步调用。 303 304**系统能力**: SystemCapability.Notification.ReminderAgent 305 306**参数**: 307 308| 参数名 | 类型 | 必填 | 说明 | 309| -------- | -------- | -------- | -------- | 310| slot | [NotificationSlot](js-apis-notification.md#notificationslot) | 是 | notification slot实例,仅支持设置其type属性。 | 311 312**返回值**: 313 314| 类型 | 说明 | 315| -------- | -------- | 316| Promise<void> | Promise类型异步回调。 | 317 318**示例**: 319 320```js 321import notification from '@ohos.notification' 322 323let mySlot = { 324 type: notification.SlotType.SOCIAL_COMMUNICATION 325} 326reminderAgent.addNotificationSlot(mySlot).then(() => { 327 console.log("addNotificationSlot promise"); 328}); 329``` 330 331 332## reminderAgent.removeNotificationSlot 333 334removeNotificationSlot(slotType: notification.SlotType, callback: AsyncCallback<void>): void 335 336删除目标NotificationSlot,使用callback方式实现异步调用。 337 338**系统能力**: SystemCapability.Notification.ReminderAgent 339 340**参数**: 341 342| 参数名 | 类型 | 必填 | 说明 | 343| -------- | -------- | -------- | -------- | 344| slotType | [notification.SlotType](js-apis-notification.md#slottype) | 是 | 目标notification slot的类型。 | 345| callback | AsyncCallback<void> | 是 | 异步回调。 | 346 347**示例**: 348 349```js 350import notification from '@ohos.notification' 351 352reminderAgent.removeNotificationSlot(notification.SlotType.CONTENT_INFORMATION, (err, data) => { 353 console.log("removeNotificationSlot callback"); 354}); 355``` 356 357 358## reminderAgent.removeNotificationSlot 359 360removeNotificationSlot(slotType: notification.SlotType): Promise<void> 361 362删除目标NotificationSlot,使用Promise方式实现异步调用。 363 364**系统能力**: SystemCapability.Notification.ReminderAgent 365 366**参数**: 367 368| 参数名 | 类型 | 必填 | 说明 | 369| -------- | -------- | -------- | -------- | 370| slotType | [notification.SlotType](js-apis-notification.md#slottype) | 是 | 目标notification slot的类型。 | 371 372**返回值**: 373 374| 类型 | 说明 | 375| -------- | -------- | 376| Promise<void> | Promise类型异步回调。 | 377 378**示例**: 379 380```js 381import notification from '@ohos.notification' 382 383reminderAgent.removeNotificationSlot(notification.SlotType.CONTENT_INFORMATION).then(() => { 384 console.log("removeNotificationSlot promise"); 385}); 386``` 387 388 389## ActionButtonType 390 391按钮的类型。 392 393**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 394 395| 名称 | 值 | 说明 | 396| -------- | -------- | -------- | 397| ACTION_BUTTON_TYPE_CLOSE | 0 | 表示关闭提醒的按钮。 | 398| ACTION_BUTTON_TYPE_SNOOZE | 1 | 表示延迟提醒的按钮。 | 399 400 401## ReminderType 402 403提醒的类型。 404 405**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 406 407| 名称 | 值 | 说明 | 408| -------- | -------- | -------- | 409| REMINDER_TYPE_TIMER | 0 | 表示提醒类型:倒计时。 | 410| REMINDER_TYPE_CALENDAR | 1 | 表示提醒类型:日历。 | 411| REMINDER_TYPE_ALARM | 2 | 表示提醒类型:闹钟。 | 412 413 414## ActionButton 415 416用于设置弹出的提醒通知信息上显示的按钮类型和标题。 417 418**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 419 420| 名称 | 类型 | 必填 | 说明 | 421| -------- | -------- | -------- | -------- | 422| title | string | 是 | 按钮显示的标题。 | 423| type | [ActionButtonType](#actionbuttontype) | 是 | 按钮的类型。 | 424 425 426## WantAgent 427 428点击提醒通知后跳转的目标ability信息。 429 430**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 431 432| 名称 | 类型 | 必填 | 说明 | 433| -------- | -------- | -------- | -------- | 434| pkgName | string | 是 | 指明点击提醒通知栏后跳转的目标hap包名。 | 435| abilityName | string | 是 | 指明点击提醒通知栏后跳转的目标ability名称。 | 436 437 438## MaxScreenWantAgent 439 440全屏显示提醒到达时自动拉起的目标ability信息,该接口预留。 441 442**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 443 444| 名称 | 类型 | 必填 | 说明 | 445| -------- | -------- | -------- | -------- | 446| pkgName | string | 是 | 指明提醒到达时自动拉起的目标hap包名(如果设备在使用中,则只弹出通知横幅框)。 | 447| abilityName | string | 是 | 指明提醒到达时自动拉起的目标ability名(如果设备在使用中,则只弹出通知横幅框)。 | 448 449 450## ReminderRequest 451 452提醒实例对象,用于设置提醒类型、响铃时长等具体信息。 453 454**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 455 456| 名称 | 类型 | 必填 | 说明 | 457| -------- | -------- | -------- | -------- | 458| reminderType | [ReminderType](#remindertype) | 是 | 指明提醒类型。 | 459| actionButton | [ActionButton](#actionbutton) | 否 | 弹出的提醒通知栏中显示的按钮(参数可选,支持0/1/2个按钮)。 | 460| wantAgent | [WantAgent](#wantagent) | 否 | 点击通知后需要跳转的目标ability信息。 | 461| maxScreenWantAgent | [MaxScreenWantAgent](#maxscreenwantagent) | 否 | 提醒到达时跳转的目标包。如果设备正在使用中,则弹出一个通知框。 | 462| ringDuration | number | 否 | 指明响铃时长(单位:秒),默认1秒。 | 463| snoozeTimes | number | 否 | 指明延迟提醒次数,默认0次。 | 464| timeInterval | number | 否 | 执行延迟提醒间隔(单位:秒),默认0秒。 | 465| title | string | 否 | 指明提醒标题。 | 466| content | string | 否 | 指明提醒内容。 | 467| expiredContent | string | 否 | 指明提醒过期后需要显示的内容。 | 468| snoozeContent | string | 否 | 指明延迟提醒时需要显示的内容。 | 469| notificationId | number | 否 | 指明提醒使用的通知的id号,相同id号的提醒会覆盖。 | 470| slotType | [notification.SlotType](js-apis-notification.md#slottype) | 否 | 指明提醒的slot类型。 | 471 472 473## ReminderRequestCalendar 474 475ReminderRequestCalendar extends ReminderRequest 476 477日历实例对象,用于设置提醒的时间。 478 479**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 480 481| 名称 | 类型 | 必填 | 说明 | 482| -------- | -------- | -------- | -------- | 483| dateTime | [LocalDateTime](#localdatetime) | 是 | 指明提醒的目标时间。 | 484| repeatMonths | Array<number> | 否 | 指明重复提醒的月份。 | 485| repeatDays | Array<number> | 否 | 指明重复提醒的日期。 | 486 487 488## ReminderRequestAlarm 489 490ReminderRequestAlarm extends ReminderRequest 491 492闹钟实例对象,用于设置提醒的时间。 493 494**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 495 496| 名称 | 类型 | 必填 | 说明 | 497| -------- | -------- | -------- | -------- | 498| hour | number | 是 | 指明提醒的目标时刻。 | 499| minute | number | 是 | 指明提醒的目标分钟。 | 500| daysOfWeek | Array<number> | 否 | 指明每周哪几天需要重复提醒。范围为周一到周末,对应数字为1到7。 | 501 502 503## ReminderRequestTimer 504 505ReminderRequestTimer extends ReminderRequest 506 507倒计时实例对象,用于设置提醒的时间。 508 509**系统能力**:SystemCapability.Notification.ReminderAgent 510 511| 名称 | 类型 | 必填 | 说明 | 512| -------- | -------- | -------- | -------- | 513| triggerTimeInSeconds | number | 是 | 指明倒计时的秒数。 | 514 515 516## LocalDateTime 517 518用于日历类提醒设置时指定时间信息。 519 520**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.ReminderAgent 521 522| 名称 | 类型 | 必填 | 说明 | 523| -------- | -------- | -------- | -------- | 524| year | number | 是 | 年 | 525| month | number | 是 | 月 | 526| day | number | 是 | 日 | 527| hour | number | 是 | 时 | 528| minute | number | 是 | 分 | 529| second | number | 否 | 秒 | 530