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