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