1# @ohos.notificationSubscribe (NotificationSubscribe模块)(系统接口) 2 3本模块提供通知订阅、取消订阅、通知移除等,一般情况下,只有系统应用具有这些操作权限。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口均为系统接口。 10 11## 导入模块 12 13```ts 14import { notificationSubscribe } from '@kit.NotificationKit'; 15``` 16 17## notificationSubscribe.subscribe 18 19subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback\<void\>): void 20 21订阅通知并指定订阅信息。使用callback异步回调。 22 23**系统能力**:SystemCapability.Notification.Notification 24 25**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 26 27**系统接口**: 此接口为系统接口。 28 29**参数:** 30 31| 参数名 | 类型 | 必填 | 说明 | 32| ---------- | ------------------------- | ---- | ---------------- | 33| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是 | 通知订阅对象。 | 34| info | [NotificationSubscribeInfo](js-apis-inner-notification-notificationSubscribeInfo-sys.md#notificationsubscribeinfo) | 是 | 通知订阅信息。 | 35| callback | AsyncCallback\<void\> | 是 | 订阅动作回调函数。 | 36 37**错误码:** 38 39以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 40 41| 错误码ID | 错误信息 | 42| -------- | ----------------------------------- | 43| 201 | Permission denied. | 44| 202 | Not system application to call the interface. | 45| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 46| 1600001 | Internal error. | 47| 1600002 | Marshalling or unmarshalling error. | 48| 1600003 | Failed to connect service. | 49| 1600012 | No memory space. | 50 51**示例:** 52 53```ts 54import { BusinessError } from '@kit.BasicServicesKit'; 55 56//subscribe回调 57let subscribeCallback = (err: BusinessError) => { 58 if (err) { 59 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 60 } else { 61 console.info("subscribe success"); 62 } 63} 64let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => { 65 console.info("Consume callback: " + JSON.stringify(data)); 66} 67let subscriber: notificationSubscribe.NotificationSubscriber = { 68 onConsume: onConsumeCallback 69}; 70//不会对bundleNames进行校验,开发者自己确定需要订阅哪些bundleName 71let info: notificationSubscribe.NotificationSubscribeInfo = { 72 bundleNames: ["bundleName1","bundleName2"] 73}; 74notificationSubscribe.subscribe(subscriber, info, subscribeCallback); 75``` 76 77## notificationSubscribe.subscribe 78 79subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void 80 81订阅当前用户下所有应用的通知。使用callback异步回调。 82 83**系统能力**:SystemCapability.Notification.Notification 84 85**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 86 87**系统接口**: 此接口为系统接口。 88 89**参数:** 90 91| 参数名 | 类型 | 必填 | 说明 | 92| ---------- | ---------------------- | ---- | ---------------- | 93| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是 | 通知订阅对象。 | 94| callback | AsyncCallback\<void\> | 是 | 订阅动作回调函数。 | 95 96**错误码:** 97 98以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 99 100| 错误码ID | 错误信息 | 101| -------- | ----------------------------------- | 102| 201 | Permission denied. | 103| 202 | Not system application to call the interface. | 104| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 105| 1600001 | Internal error. | 106| 1600002 | Marshalling or unmarshalling error. | 107| 1600003 | Failed to connect service. | 108| 1600012 | No memory space. | 109 110**示例:** 111 112```ts 113import { BusinessError } from '@kit.BasicServicesKit'; 114 115let subscribeCallback = (err: BusinessError) => { 116 if (err) { 117 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 118 } else { 119 console.info("subscribe success"); 120 } 121} 122let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => { 123 console.info("Consume callback: " + JSON.stringify(data)); 124} 125let subscriber: notificationSubscribe.NotificationSubscriber = { 126 onConsume: onConsumeCallback 127}; 128notificationSubscribe.subscribe(subscriber, subscribeCallback); 129``` 130 131 132 133## notificationSubscribe.subscribe 134 135subscribe(subscriber: NotificationSubscriber, info?: NotificationSubscribeInfo): Promise\<void\> 136 137订阅通知并指定订阅信息。使用Promise异步回调。 138 139**系统能力**:SystemCapability.Notification.Notification 140 141**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 142 143**系统接口**: 此接口为系统接口。 144 145**参数:** 146 147| 参数名 | 类型 | 必填 | 说明 | 148| ---------- | ------------------------- | ---- | ------------ | 149| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是 | 通知订阅对象。 | 150| info | [NotificationSubscribeInfo](js-apis-inner-notification-notificationSubscribeInfo-sys.md#notificationsubscribeinfo) | 否 | 通知订阅信息,默认为空(当为空时,表示订阅当前用户下所有应用的通知,否则表示订阅通知并指定订阅信息)。 | 151 152**返回值:** 153 154| 类型 | 说明 | 155| ------- |------------------| 156| Promise\<void\> | 无返回结果的Promise对象。 | 157 158**错误码:** 159 160以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 161 162| 错误码ID | 错误信息 | 163| -------- | ----------------------------------- | 164| 201 | Permission denied. | 165| 202 | Not system application to call the interface. | 166| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 167| 1600001 | Internal error. | 168| 1600002 | Marshalling or unmarshalling error. | 169| 1600003 | Failed to connect service. | 170| 1600012 | No memory space. | 171 172**示例:** 173 174```ts 175import { BusinessError } from '@kit.BasicServicesKit'; 176 177let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => { 178 console.info("Consume callback: " + JSON.stringify(data)); 179} 180let subscriber: notificationSubscribe.NotificationSubscriber = { 181 onConsume: onConsumeCallback 182}; 183notificationSubscribe.subscribe(subscriber).then(() => { 184 console.info("subscribe success"); 185}).catch((err: BusinessError) => { 186 console.error("subscribe fail: " + JSON.stringify(err)); 187}); 188``` 189 190 191## notificationSubscribe.subscribeSelf<sup>11+</sup> 192 193subscribeSelf(subscriber: NotificationSubscriber): Promise\<void\> 194 195订阅本应用的通知并指定订阅信息。使用Promise异步回调。 196 197**系统能力**:SystemCapability.Notification.Notification 198 199**系统接口**: 此接口为系统接口。 200 201**参数:** 202 203| 参数名 | 类型 | 必填 | 说明 | 204| ---------- | ------------------------- | ---- | ------------ | 205| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是 | 通知订阅对象。 | 206 207**返回值:** 208 209| 类型 | 说明 | 210| ------- |------------------| 211| Promise\<void\> | 无返回结果的Promise对象。 | 212 213**错误码:** 214 215以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 216 217| 错误码ID | 错误信息 | 218| -------- | ----------------------------------- | 219| 201 | Permission denied. | 220| 202 | Not system application to call the interface. | 221| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 222| 1600001 | Internal error. | 223| 1600002 | Marshalling or unmarshalling error. | 224| 1600003 | Failed to connect service. | 225| 1600012 | No memory space. | 226 227**示例:** 228 229```ts 230import { BusinessError } from '@kit.BasicServicesKit'; 231 232let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => { 233 console.info("Consume callback: " + JSON.stringify(data)); 234} 235let subscriber: notificationSubscribe.NotificationSubscriber = { 236 onConsume: onConsumeCallback 237}; 238notificationSubscribe.subscribeSelf(subscriber).then(() => { 239 console.info("subscribeSelf success"); 240}).catch((err: BusinessError) => { 241 console.error("subscribeSelf fail: " + JSON.stringify(err)); 242}); 243``` 244 245 246 247## notificationSubscribe.unsubscribe 248 249unsubscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void 250 251取消订阅。使用callback异步回调。 252 253**系统能力**:SystemCapability.Notification.Notification 254 255**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 256 257**系统接口**: 此接口为系统接口。 258 259**参数:** 260 261| 参数名 | 类型 | 必填 | 说明 | 262| ---------- | ---------------------- | ---- | -------------------- | 263| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是 | 通知订阅对象。 | 264| callback | AsyncCallback\<void\> | 是 | 取消订阅动作回调函数。 | 265 266**错误码:** 267 268以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 269 270| 错误码ID | 错误信息 | 271| -------- | ----------------------------------- | 272| 201 | Permission denied. | 273| 202 | Not system application to call the interface. | 274| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 275| 1600001 | Internal error. | 276| 1600002 | Marshalling or unmarshalling error. | 277| 1600003 | Failed to connect service. | 278 279**示例:** 280 281```ts 282import { BusinessError } from '@kit.BasicServicesKit'; 283 284let unsubscribeCallback = (err: BusinessError) => { 285 if (err) { 286 console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`); 287 } else { 288 console.info("unsubscribe success"); 289 } 290} 291let onDisconnectCallback = () => { 292 console.info("subscribe disconnect"); 293} 294let subscriber: notificationSubscribe.NotificationSubscriber = { 295 onDisconnect: onDisconnectCallback 296}; 297notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback); 298``` 299 300## notificationSubscribe.unsubscribe 301 302unsubscribe(subscriber: NotificationSubscriber): Promise\<void\> 303 304取消订阅。使用Promise异步回调。 305 306**系统能力**:SystemCapability.Notification.Notification 307 308**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 309 310**系统接口**: 此接口为系统接口。 311 312**参数:** 313 314| 参数名 | 类型 | 必填 | 说明 | 315| ---------- | ---------------------- | ---- | ------------ | 316| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是 | 通知订阅对象。 | 317 318**返回值:** 319 320| 类型 | 说明 | 321| ------- |------------| 322| Promise\<void\> | 无返回结果的Promise对象。 | 323 324**错误码:** 325 326以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 327 328| 错误码ID | 错误信息 | 329| -------- | ----------------------------------- | 330| 201 | Permission denied. | 331| 202 | Not system application to call the interface. | 332| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 333| 1600001 | Internal error. | 334| 1600002 | Marshalling or unmarshalling error. | 335| 1600003 | Failed to connect service. | 336 337**示例:** 338 339```ts 340import { BusinessError } from '@kit.BasicServicesKit'; 341 342let onDisconnectCallback = () => { 343 console.info("subscribe disconnect"); 344} 345let subscriber: notificationSubscribe.NotificationSubscriber = { 346 onDisconnect: onDisconnectCallback 347}; 348notificationSubscribe.unsubscribe(subscriber).then(() => { 349 console.info("unsubscribe success"); 350}).catch((err: BusinessError) => { 351 console.error("unsubscribe fail: " + JSON.stringify(err)); 352}); 353``` 354 355## notificationSubscribe.remove 356 357remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason, callback: AsyncCallback\<void\>): void 358 359根据应用的包信息和通知键值,删除指定通知。使用callback异步回调。 360 361**系统能力**:SystemCapability.Notification.Notification 362 363**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 364 365**系统接口**: 此接口为系统接口。 366 367**参数:** 368 369| 参数名 | 类型 | 必填 | 说明 | 370| --------------- | ----------------------------------| ---- | -------------------- | 371| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | 是 | 指定应用的包信息。 | 372| notificationKey | [NotificationKey](#notificationkey) | 是 | 通知键值。 | 373| reason | [RemoveReason](#removereason) | 是 | 通知删除原因。 | 374| callback | AsyncCallback\<void\> | 是 | 删除指定通知回调函数。 | 375 376**错误码:** 377 378以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 379 380| 错误码ID | 错误信息 | 381| -------- | ---------------------------------------- | 382| 201 | Permission denied. | 383| 202 | Not system application to call the interface. | 384| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 385| 1600001 | Internal error. | 386| 1600002 | Marshalling or unmarshalling error. | 387| 1600003 | Failed to connect service. | 388| 1600007 | The notification is not exist. | 389| 17700001 | The specified bundle name was not found. | 390 391**示例:** 392 393```ts 394import { BusinessError } from '@kit.BasicServicesKit'; 395import { notificationManager } from '@kit.NotificationKit'; 396 397let removeCallback = (err: BusinessError) => { 398 if (err) { 399 console.error(`remove failed, code is ${err.code}, message is ${err.message}`); 400 } else { 401 console.info("remove success"); 402 } 403} 404let bundle: notificationManager.BundleOption = { 405 bundle: "bundleName1", 406}; 407let notificationKey: notificationSubscribe.NotificationKey = { 408 id: 0, 409 label: "label", 410}; 411let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE; 412notificationSubscribe.remove(bundle, notificationKey, reason, removeCallback); 413``` 414 415 416 417## notificationSubscribe.remove 418 419remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason): Promise\<void\> 420 421根据应用的包信息和通知键值,删除指定通知。使用Promise异步回调。 422 423**系统能力**:SystemCapability.Notification.Notification 424 425**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 426 427**系统接口**: 此接口为系统接口。 428 429**参数:** 430 431| 参数名 | 类型 | 必填 | 说明 | 432| --------------- | --------------- | ---- | ---------- | 433| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | 是 | 指定应用的包信息。 | 434| notificationKey | [NotificationKey](#notificationkey) | 是 | 通知键值。 | 435| reason | [RemoveReason](#removereason) | 是 | 通知删除原因。 | 436 437**返回值:** 438 439| 类型 | 说明 | 440| ------- |------------| 441| Promise\<void\> | 无返回结果的Promise对象。 | 442 443**错误码:** 444 445以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 446 447| 错误码ID | 错误信息 | 448| -------- | ---------------------------------------- | 449| 201 | Permission denied. | 450| 202 | Not system application to call the interface. | 451| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 452| 1600001 | Internal error. | 453| 1600002 | Marshalling or unmarshalling error. | 454| 1600003 | Failed to connect service. | 455| 1600007 | The notification is not exist. | 456| 17700001 | The specified bundle name was not found. | 457 458**示例:** 459 460```ts 461import { BusinessError } from '@kit.BasicServicesKit'; 462import { notificationManager } from '@kit.NotificationKit'; 463 464let bundle: notificationManager.BundleOption = { 465 bundle: "bundleName1", 466}; 467let notificationKey: notificationSubscribe.NotificationKey = { 468 id: 0, 469 label: "label", 470}; 471let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE; 472notificationSubscribe.remove(bundle, notificationKey, reason).then(() => { 473 console.info("remove success"); 474}).catch((err: BusinessError) => { 475 console.error("remove fail: " + JSON.stringify(err)); 476}); 477``` 478 479## notificationSubscribe.remove 480 481remove(hashCode: string, reason: RemoveReason, callback: AsyncCallback\<void\>): void 482 483通过通知的唯一ID,删除指定通知。使用callback异步回调。 484 485**系统能力**:SystemCapability.Notification.Notification 486 487**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 488 489**系统接口**: 此接口为系统接口。 490 491**参数:** 492 493| 参数名 | 类型 | 必填 | 说明 | 494| -------- | --------------------- | ---- | -------------------- | 495| hashCode | string | 是 | 通知唯一ID。可以通过[onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume)回调的入参[SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata)获取其内部[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象中的hashCode。 | 496| reason | [RemoveReason](#removereason) | 是 | 通知删除原因。 | 497| callback | AsyncCallback\<void\> | 是 | 删除指定通知回调函数。 | 498 499**错误码:** 500 501以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 502 503| 错误码ID | 错误信息 | 504| -------- | ----------------------------------- | 505| 201 | Permission denied. | 506| 202 | Not system application to call the interface. | 507| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 508| 1600001 | Internal error. | 509| 1600002 | Marshalling or unmarshalling error. | 510| 1600003 | Failed to connect service. | 511| 1600007 | The notification is not exist. | 512 513**示例:** 514 515```ts 516import { BusinessError } from '@kit.BasicServicesKit'; 517 518let hashCode: string = 'hashCode'; 519let removeCallback = (err: BusinessError) => { 520 if (err) { 521 console.error(`remove failed, code is ${err.code}, message is ${err.message}`); 522 } else { 523 console.info("remove success"); 524 } 525} 526let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE; 527notificationSubscribe.remove(hashCode, reason, removeCallback); 528``` 529 530## notificationSubscribe.remove 531 532remove(hashCode: string, reason: RemoveReason): Promise\<void\> 533 534通过通知的唯一ID,删除指定通知。使用Promise异步回调。 535 536**系统能力**:SystemCapability.Notification.Notification 537 538**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 539 540**系统接口**: 此接口为系统接口。 541 542**参数:** 543 544| 参数名 | 类型 | 必填 | 说明 | 545| -------- | ---------- | ---- | ---------- | 546| hashCode | string | 是 | 通知唯一ID。可以通过[onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume)回调的入参[SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata)获取其内部[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象中的hashCode。 | 547| reason | [RemoveReason](#removereason) | 是 | 通知删除原因。 | 548 549**返回值:** 550 551| 类型 | 说明 | 552| ------- |--| 553| Promise\<void\> | 无返回结果的Promise对象。 | 554 555**错误码:** 556 557以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 558 559| 错误码ID | 错误信息 | 560| -------- | ----------------------------------- | 561| 201 | Permission denied. | 562| 202 | Not system application to call the interface. | 563| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 564| 1600001 | Internal error. | 565| 1600002 | Marshalling or unmarshalling error. | 566| 1600003 | Failed to connect service. | 567| 1600007 | The notification is not exist. | 568 569**示例:** 570 571```ts 572import { BusinessError } from '@kit.BasicServicesKit'; 573 574let hashCode: string = 'hashCode'; 575let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE; 576notificationSubscribe.remove(hashCode, reason).then(() => { 577 console.info("remove success"); 578}).catch((err: BusinessError) => { 579 console.error("remove fail: " + JSON.stringify(err)); 580}); 581``` 582 583## notificationSubscribe.remove<sup>10+</sup> 584 585remove(hashCodes: Array\<String\>, reason: RemoveReason, callback: AsyncCallback\<void\>): void 586 587批量删除指定通知。使用callback异步回调。 588 589**系统能力**:SystemCapability.Notification.Notification 590 591**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 592 593**系统接口**: 此接口为系统接口。 594 595**参数:** 596 597| 参数名 | 类型 | 必填 | 说明 | 598|-----------|-------------------------------| ---- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 599| hashCodes | Array\<String\> | 是 | 通知唯一ID数组集合。可以通过[onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume)回调的入参[SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata)获取其内部[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象中的hashCode。 | 600| reason | [RemoveReason](#removereason) | 是 | 通知删除原因。 | 601| callback | AsyncCallback\<void\> | 是 | 删除指定通知回调函数。 | 602 603**错误码:** 604 605以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 606 607| 错误码ID | 错误信息 | 608| -------- | ----------------------------------- | 609| 201 | Permission denied. | 610| 202 | Not system application to call the interface. | 611| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 612| 1600001 | Internal error. | 613| 1600002 | Marshalling or unmarshalling error. | 614| 1600003 | Failed to connect service. | 615 616**示例:** 617 618```ts 619import { BusinessError } from '@kit.BasicServicesKit'; 620 621let hashCodes: string[] = ['hashCode1', 'hashCode2']; 622let removeCallback = (err: BusinessError) => { 623 if (err) { 624 console.error(`remove failed, code is ${err.code}, message is ${err.message}`); 625 } else { 626 console.info("remove success"); 627 } 628} 629let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE; 630notificationSubscribe.remove(hashCodes, reason, removeCallback); 631``` 632 633## notificationSubscribe.remove<sup>10+</sup> 634 635remove(hashCodes: Array\<String\>, reason: RemoveReason): Promise\<void\> 636 637批量删除指定通知。使用Promise异步回调。 638 639**系统能力**:SystemCapability.Notification.Notification 640 641**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 642 643**系统接口**: 此接口为系统接口。 644 645**参数:** 646 647| 参数名 | 类型 | 必填 | 说明 | 648|-----------|-------------------------------| ---- |-------------| 649| hashCodes | Array\<String\> | 是 | 通知唯一ID数组集合。 | 650| reason | [RemoveReason](#removereason) | 是 | 通知删除原因。 | 651 652**返回值:** 653 654| 类型 | 说明 | 655| ------- |------------------| 656| Promise\<void\> | 无返回结果的Promise对象。 | 657 658**错误码:** 659 660以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 661 662| 错误码ID | 错误信息 | 663| -------- | ----------------------------------- | 664| 201 | Permission denied. | 665| 202 | Not system application to call the interface. | 666| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 667| 1600001 | Internal error. | 668| 1600002 | Marshalling or unmarshalling error. | 669| 1600003 | Failed to connect service. | 670 671**示例:** 672 673```ts 674import { BusinessError } from '@kit.BasicServicesKit'; 675 676let hashCodes: string[] = ['hashCode1','hashCode2']; 677let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE; 678notificationSubscribe.remove(hashCodes, reason).then(() => { 679 console.info("remove success"); 680}).catch((err: BusinessError) => { 681 console.error("remove fail: " + JSON.stringify(err)); 682}); 683``` 684 685## notificationSubscribe.removeAll 686 687removeAll(bundle: BundleOption, callback: AsyncCallback\<void\>): void 688 689删除指定应用的所有通知。使用callback异步回调。 690 691**系统能力**:SystemCapability.Notification.Notification 692 693**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 694 695**系统接口**:此接口为系统接口。 696 697**参数:** 698 699| 参数名 | 类型 | 必填 | 说明 | 700| -------- | --------------------- | ---- | ---------------------------- | 701| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | 是 | 指定应用的包信息。 | 702| callback | AsyncCallback\<void\> | 是 | 删除指定应用的所有通知回调函数。 | 703 704**错误码:** 705 706以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 707 708| 错误码ID | 错误信息 | 709| -------- | ---------------------------------------- | 710| 201 | Permission denied. | 711| 202 | Not system application to call the interface. | 712| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 713| 1600001 | Internal error. | 714| 1600002 | Marshalling or unmarshalling error. | 715| 1600003 | Failed to connect service. | 716| 17700001 | The specified bundle name was not found. | 717 718**示例:** 719 720```ts 721import { BusinessError } from '@kit.BasicServicesKit'; 722 723let removeAllCallback = (err: BusinessError) => { 724 if (err) { 725 console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`); 726 } else { 727 console.info("removeAll success"); 728 } 729} 730let bundle: notificationSubscribe.BundleOption = { 731 bundle: "bundleName1", 732}; 733notificationSubscribe.removeAll(bundle, removeAllCallback); 734``` 735 736## notificationSubscribe.removeAll 737 738removeAll(callback: AsyncCallback\<void\>): void 739 740删除所有通知。使用callback异步回调。 741 742**系统能力**:SystemCapability.Notification.Notification 743 744**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 745 746**系统接口**: 此接口为系统接口。 747 748**参数:** 749 750| 参数名 | 类型 | 必填 | 说明 | 751| -------- | --------------------- | ---- | -------------------- | 752| callback | AsyncCallback\<void\> | 是 | 删除所有通知回调函数。 | 753 754**错误码:** 755 756以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 757 758| 错误码ID | 错误信息 | 759| -------- | ----------------------------------- | 760| 201 | Permission denied. | 761| 202 | Not system application to call the interface. | 762| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 763| 1600001 | Internal error. | 764| 1600002 | Marshalling or unmarshalling error. | 765| 1600003 | Failed to connect service. | 766 767**示例:** 768 769```ts 770import { BusinessError } from '@kit.BasicServicesKit'; 771 772let removeAllCallback = (err: BusinessError) => { 773 if (err) { 774 console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`); 775 } else { 776 console.info("removeAll success"); 777 } 778} 779notificationSubscribe.removeAll(removeAllCallback); 780``` 781 782## notificationSubscribe.removeAll 783 784removeAll(bundle?: BundleOption): Promise\<void\> 785 786删除指定应用的所有通知。使用Promise异步回调。 787 788**系统能力**:SystemCapability.Notification.Notification 789 790**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 791 792**系统接口**: 此接口为系统接口。 793 794**参数:** 795 796| 参数名 | 类型 | 必填 | 说明 | 797| ------ | ------------ | ---- | ---------- | 798| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | 否 | 指定应用的包信息。默认为空,表示删除所有通知。 | 799 800**返回值:** 801 802| 类型 | 说明 | 803| ------- |------------| 804| Promise\<void\> | 无返回结果的Promise对象。 | 805 806**错误码:** 807 808以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 809 810| 错误码ID | 错误信息 | 811| -------- | ---------------------------------------- | 812| 201 | Permission denied. | 813| 202 | Not system application to call the interface. | 814| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 815| 1600001 | Internal error. | 816| 1600002 | Marshalling or unmarshalling error. | 817| 1600003 | Failed to connect service. | 818| 17700001 | The specified bundle name was not found. | 819 820**示例:** 821 822```ts 823import { BusinessError } from '@kit.BasicServicesKit'; 824 825// 不指定应用时,删除所有通知 826notificationSubscribe.removeAll().then(() => { 827 console.info("removeAll success"); 828}).catch((err: BusinessError) => { 829 console.error("removeAll fail: " + JSON.stringify(err)); 830}); 831``` 832 833## notificationSubscribe.removeAll 834 835removeAll(userId: number, callback: AsyncCallback\<void>): void 836 837删除指定用户下的所有通知。使用callback异步回调。 838 839**系统能力**:SystemCapability.Notification.Notification 840 841**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 842 843**系统接口**: 此接口为系统接口。 844 845**参数:** 846 847| 参数名 | 类型 | 必填 | 说明 | 848| ------ | ------------ | ---- | ---------- | 849| userId | number | 是 | 用户ID。 | 850| callback | AsyncCallback\<void\> | 是 | 删除指定用户所有通知回调函数。 | 851 852**错误码:** 853 854以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 855 856| 错误码ID | 错误信息 | 857| -------- | ----------------------------------- | 858| 201 | Permission denied. | 859| 202 | Not system application to call the interface. | 860| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 861| 1600001 | Internal error. | 862| 1600002 | Marshalling or unmarshalling error. | 863| 1600003 | Failed to connect service. | 864| 1600008 | The user does not exist. | 865 866**示例:** 867 868```ts 869import { BusinessError } from '@kit.BasicServicesKit'; 870 871let removeAllCallback = (err: BusinessError) => { 872 if (err) { 873 console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`); 874 } else { 875 console.info("removeAll success"); 876 } 877} 878// 用户ID,使用时需替换为真实的userId。 879let userId: number = 1; 880notificationSubscribe.removeAll(userId, removeAllCallback); 881``` 882 883## notificationSubscribe.removeAll 884 885removeAll(userId: number): Promise\<void> 886 887删除指定用户下的所有通知。使用Promise异步回调。 888 889**系统能力**:SystemCapability.Notification.Notification 890 891**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER 892 893**系统接口**: 此接口为系统接口。 894 895**参数:** 896 897| 参数名 | 类型 | 必填 | 说明 | 898| ------ | ------------ | ---- | ---------- | 899| userId | number | 是 | 用户ID。 | 900 901**错误码:** 902 903以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 904 905| 错误码ID | 错误信息 | 906| -------- | ----------------------------------- | 907| 201 | Permission denied. | 908| 202 | Not system application to call the interface. | 909| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 910| 1600001 | Internal error. | 911| 1600002 | Marshalling or unmarshalling error. | 912| 1600003 | Failed to connect service. | 913| 1600008 | The user does not exist. | 914 915**示例:** 916 917```ts 918import { BusinessError } from '@kit.BasicServicesKit'; 919 920let userId: number = 1; 921notificationSubscribe.removeAll(userId).then(() => { 922 console.info("removeAll success"); 923}).catch((err: BusinessError) => { 924 console.error("removeAll fail: " + JSON.stringify(err)); 925}); 926``` 927 928## NotificationKey 929 930**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification 931 932**系统接口**: 此接口为系统接口。 933 934| 名称 | 类型 | 必填 | 说明 | 935| ----- | ------ | --- | -------- | 936| id | number | 是 | 通知ID。 | 937| label | string | 否 | 通知标签,默认为空。 | 938 939## RemoveReason 940 941**系统能力**:SystemCapability.Notification.Notification 942 943**系统接口**: 此接口为系统接口。 944 945| 名称 | 值 | 说明 | 946| -------------------- | --- | -------------------- | 947| CLICK_REASON_REMOVE | 1 | 点击通知后删除通知。 | 948| CANCEL_REASON_REMOVE | 2 | 用户删除通知。 | 949