1# NotificationSubscriber(系统接口) 2 3作为订阅通知接口[subscribe](./js-apis-notificationSubscribe-sys.md)的入参,提供订阅者接收到新通知、取消通知等的回调方法。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块为系统接口。 10 11## 导入模块 12 13```js 14import { notificationSubscribe } from '@kit.NotificationKit'; 15``` 16 17## onConsume 18 19onConsume?: (data: SubscribeCallbackData) => void 20 21接收到新通知的回调函数。 22 23**系统能力**:SystemCapability.Notification.Notification 24 25**系统接口**: 此接口为系统接口。 26 27**参数:** 28 29| 参数名 | 类型 | 必填 | 说明 | 30| ------------ | ------------------------ | ---- | -------------------------- | 31| onConsume | (data: [SubscribeCallbackData](#subscribecallbackdata)) => void | 否 | 新接收到的通知信息。 | 32 33**示例:** 34 35```ts 36import { BusinessError } from '@kit.BasicServicesKit'; 37 38let subscribeCallback = (err: BusinessError) => { 39 if (err) { 40 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 41 } else { 42 console.info("subscribeCallback"); 43 } 44}; 45 46let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => { 47 console.info('===> onConsume in test'); 48 let req = data.request; 49 console.info('===> onConsume callback req.id:' + req.id); 50}; 51 52let subscriber: notificationSubscribe.NotificationSubscriber = { 53 onConsume: onConsumeCallback 54}; 55 56notificationSubscribe.subscribe(subscriber, subscribeCallback); 57``` 58 59## onCancel 60 61onCancel?: (data: SubscribeCallbackData) => void 62 63取消通知的回调函数。 64 65**系统能力**:SystemCapability.Notification.Notification 66 67**系统接口**: 此接口为系统接口。 68 69**参数:** 70 71| 参数名 | 类型 | 必填 | 说明 | 72| ------------ | ------------------------ | ---- | -------------------------- | 73| onCancel | (data: [SubscribeCallbackData](#subscribecallbackdata)) => void | 否 | 需要取消的通知信息。 | 74 75**示例:** 76 77```ts 78import { BusinessError } from '@kit.BasicServicesKit'; 79 80let subscribeCallback = (err: BusinessError) => { 81 if (err) { 82 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 83 } else { 84 console.info("subscribeCallback"); 85 } 86}; 87 88let onCancelCallback = (data: notificationSubscribe.SubscribeCallbackData) => { 89 console.info('===> onCancel in test'); 90 let req = data.request; 91 console.info('===> onCancel callback req.id:' + req.id); 92} 93 94let subscriber: notificationSubscribe.NotificationSubscriber = { 95 onCancel: onCancelCallback 96}; 97 98notificationSubscribe.subscribe(subscriber, subscribeCallback); 99``` 100 101## onUpdate 102 103onUpdate?: (data: NotificationSortingMap) => void 104 105更新通知排序的回调函数。 106 107**系统能力**:SystemCapability.Notification.Notification 108 109**系统接口**: 此接口为系统接口。 110 111**参数:** 112 113| 参数名 | 类型 | 必填 | 说明 | 114| ------------ | ------------------------ | ---- | -------------------------- | 115| onUpdate | (data: [NotificationSortingMap](js-apis-inner-notification-notificationSortingMap-sys.md)) => void | 否 | 最新的通知排序列表。 | 116 117**示例:** 118 119```ts 120import { BusinessError } from '@kit.BasicServicesKit'; 121 122let subscribeCallback = (err: BusinessError) => { 123 if (err) { 124 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 125 } else { 126 console.info("subscribeCallback"); 127 } 128}; 129 130let subscriber: notificationSubscribe.NotificationSubscriber = { 131 onUpdate: (map) => { 132 console.info('===> onUpdateCallback map:' + JSON.stringify(map)); 133 } 134}; 135 136notificationSubscribe.subscribe(subscriber, subscribeCallback); 137``` 138 139## onConnect 140 141onConnect?: () => void 142 143订阅完成的回调函数。 144 145**系统能力**:SystemCapability.Notification.Notification 146 147**系统接口**: 此接口为系统接口。 148 149**参数:** 150 151| 参数名 | 类型 | 必填 | 说明 | 152| ------------ | ------------------------ | ---- | -------------------------- | 153| onConnect | () => void | 否 | 订阅完成的回调。 | 154 155**示例:** 156 157```ts 158import { BusinessError } from '@kit.BasicServicesKit'; 159 160let subscribeCallback = (err: BusinessError) => { 161 if (err) { 162 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 163 } else { 164 console.info("subscribeCallback"); 165 } 166}; 167 168let onConnectCallback = () => { 169 console.info('===> onConnect in test'); 170} 171 172let subscriber: notificationSubscribe.NotificationSubscriber = { 173 onConnect: onConnectCallback 174}; 175 176notificationSubscribe.subscribe(subscriber, subscribeCallback); 177``` 178 179## onDisconnect 180 181onDisconnect?: () => void 182 183取消订阅的回调函数。 184 185**系统能力**:SystemCapability.Notification.Notification 186 187**系统接口**: 此接口为系统接口。 188 189**参数:** 190 191| 参数名 | 类型 | 必填 | 说明 | 192| ------------ | ------------------------ | ---- | -------------------------- | 193| onDisconnect | () => void | 否 | 取消订阅的回调。 | 194 195**示例:** 196 197```ts 198import { BusinessError } from '@kit.BasicServicesKit'; 199 200let subscribeCallback = (err: BusinessError) => { 201 if (err) { 202 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 203 } else { 204 console.info("subscribeCallback"); 205 } 206}; 207let unsubscribeCallback = (err: BusinessError) => { 208 if (err) { 209 console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`); 210 } else { 211 console.info("unsubscribeCallback"); 212 } 213}; 214 215let onConnectCallback = () => { 216 console.info('===> onConnect in test'); 217} 218let onDisconnectCallback = () => { 219 console.info('===> onDisconnect in test'); 220} 221 222let subscriber: notificationSubscribe.NotificationSubscriber = { 223 onConnect: onConnectCallback, 224 onDisconnect: onDisconnectCallback 225}; 226 227// 订阅通知后会收到onConnect回调 228notificationSubscribe.subscribe(subscriber, subscribeCallback); 229// 取消订阅后会收到onDisconnect回调 230notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback); 231``` 232 233## onDestroy 234 235onDestroy?: () => void 236 237服务失联回调函数。 238 239**系统能力**:SystemCapability.Notification.Notification 240 241**系统接口**: 此接口为系统接口。 242 243**参数:** 244 245| 参数名 | 类型 | 必填 | 说明 | 246| ------------ | ------------------------ | ---- | -------------------------- | 247| onDestroy | () => void | 否 | 服务失联的回调。 | 248 249**示例:** 250 251```ts 252import { BusinessError } from '@kit.BasicServicesKit'; 253 254let subscribeCallback = (err: BusinessError) => { 255 if (err) { 256 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 257 } else { 258 console.info("subscribeCallback"); 259 } 260}; 261 262let onDestroyCallback = () => { 263 console.info('===> onDestroy in test'); 264} 265 266let subscriber: notificationSubscribe.NotificationSubscriber = { 267 onDestroy: onDestroyCallback 268}; 269 270notificationSubscribe.subscribe(subscriber, subscribeCallback); 271``` 272 273## onDoNotDisturbDateChange<sup>8+</sup>(deprecated) 274 275onDoNotDisturbDateChange?: (mode: notification.DoNotDisturbDate) => void 276 277免打扰时间选项发生变更时的回调函数。 278 279> **说明:** 280> 281> 此接口从API version 8开始支持,从API version 11开始不再维护,建议使用[onDoNotDisturbChanged](js-apis-inner-notification-notificationSubscriber-sys.md#ondonotdisturbchanged11)代替。 282 283**系统能力**:SystemCapability.Notification.Notification 284 285**系统接口**: 此接口为系统接口。 286 287**参数:** 288 289| 参数名 | 类型 | 必填 | 说明 | 290| ------------ | ------------------------ | ---- | -------------------------- | 291| onDoNotDisturbDateChange | (mode: notification.[DoNotDisturbDate](js-apis-notification-sys.md#donotdisturbdate8-deprecated)) => void | 否 | 回调返回免打扰时间选项变更。 | 292 293**示例:** 294 295```ts 296import { BusinessError } from '@kit.BasicServicesKit'; 297import Notification from '@ohos.notification'; 298 299let subscribeCallback = (err: BusinessError) => { 300 if (err) { 301 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 302 } else { 303 console.info("subscribeCallback"); 304 } 305}; 306 307let onDoNotDisturbDateChangeCallback = (mode: Notification.DoNotDisturbDate) => { 308 console.info('===> onDoNotDisturbDateChange:' + mode); 309} 310 311let subscriber: notificationSubscribe.NotificationSubscriber = { 312 onDoNotDisturbDateChange: onDoNotDisturbDateChangeCallback 313}; 314 315notificationSubscribe.subscribe(subscriber, subscribeCallback); 316``` 317 318## onDoNotDisturbChanged<sup>11+</sup> 319 320onDoNotDisturbChanged?: (mode: notificationManager.DoNotDisturbDate) => void 321 322免打扰时间选项发生变更时的回调函数。 323 324**系统接口**: 此接口为系统接口。 325 326**系统能力**:SystemCapability.Notification.Notification 327 328**参数:** 329 330| 参数名 | 类型 | 必填 | 说明 | 331| ------------ | ------------------------ | ---- | -------------------------- | 332| onDoNotDisturbChanged | (mode: notificationManager.[DoNotDisturbDate](js-apis-notificationManager-sys.md#donotdisturbdate)) => void | 否 | 回调返回免打扰时间选项变更。 | 333 334**示例:** 335 336```ts 337import { BusinessError } from '@kit.BasicServicesKit'; 338import { notificationSubscribe, notificationManager } from '@kit.NotificationKit'; 339 340let subscribeCallback = (err: BusinessError) => { 341 if (err) { 342 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 343 } else { 344 console.info("subscribeCallback"); 345 } 346}; 347 348let onDoNotDisturbChangedCallback = (mode: notificationManager.DoNotDisturbDate) => { 349 console.info('===> onDoNotDisturbChanged:' + JSON.stringify(mode)); 350} 351 352let subscriber: notificationSubscribe.NotificationSubscriber = { 353 onDoNotDisturbChanged: onDoNotDisturbChangedCallback 354}; 355 356notificationSubscribe.subscribe(subscriber, subscribeCallback); 357``` 358 359## onEnabledNotificationChanged<sup>8+</sup> 360 361onEnabledNotificationChanged?: (callbackData: EnabledNotificationCallbackData) => void 362 363监听应用通知使能变化。 364 365**系统能力**:SystemCapability.Notification.Notification 366 367**系统接口**: 此接口为系统接口。 368 369**参数:** 370 371| 参数名 | 类型 | 必填 | 说明 | 372| ------------ |--------------------------------------------------------------------------------------------------------------| ---- | -------------------------- | 373| onEnabledNotificationChanged | (callbackData: [EnabledNotificationCallbackData](#enablednotificationcallbackdata8)) => void | 否 | 回调返回监听到的应用信息。 | 374 375**示例:** 376 377```ts 378import { BusinessError } from '@kit.BasicServicesKit'; 379 380let subscribeCallback = (err: BusinessError) => { 381 if (err) { 382 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 383 } else { 384 console.info("subscribeCallback"); 385 } 386}; 387 388let onEnabledNotificationChangedCallback = (callbackData: notificationSubscribe.EnabledNotificationCallbackData) => { 389 console.info("bundle: ", callbackData.bundle); 390 console.info("uid: ", callbackData.uid); 391 console.info("enable: ", callbackData.enable); 392}; 393 394let subscriber: notificationSubscribe.NotificationSubscriber = { 395 onEnabledNotificationChanged: onEnabledNotificationChangedCallback 396}; 397 398notificationSubscribe.subscribe(subscriber, subscribeCallback); 399``` 400 401## onBadgeChanged<sup>10+</sup> 402 403onBadgeChanged?: (data: BadgeNumberCallbackData) => void 404 405监听应用角标个数变化。 406 407**系统能力**:SystemCapability.Notification.Notification 408 409**系统接口**: 此接口为系统接口。 410 411**参数:** 412 413| 参数名 | 类型 | 必填 | 说明 | 414| -------- | ------------------------------------------------------------ | ---- | -------------------------- | 415| onBadgeChanged | (data: [BadgeNumberCallbackData](#badgenumbercallbackdata10)) => void | 否 | 回调返回监听到的应用信息。 | 416 417**示例:** 418 419```ts 420import { BusinessError } from '@kit.BasicServicesKit'; 421 422let subscribeCallback = (err: BusinessError) => { 423 if (err) { 424 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 425 } else { 426 console.info("subscribeCallback"); 427 } 428}; 429 430let subscriber: notificationSubscribe.NotificationSubscriber = { 431 onBadgeChanged: (data) => { 432 console.info("bundle: ", data.bundle); 433 console.info("uid: ", data.uid); 434 console.info("badgeNumber: ", data.badgeNumber); 435 } 436}; 437 438notificationSubscribe.subscribe(subscriber, subscribeCallback); 439``` 440 441## onBadgeEnabledChanged<sup>12+</sup> 442 443onBadgeEnabledChanged?: BadgeEnabledChangedCallback 444 445监听应用角标使能状态变化。 446 447**系统能力**:SystemCapability.Notification.Notification 448 449**系统接口**: 此接口为系统接口。 450 451**参数:** 452 453| 参数名 | 类型 | 必填 | 说明 | 454| -------- | ------------------------------------------------------------ | ---- | -------------------------- | 455| onBadgeEnabledChanged | [BadgeEnabledChangedCallback](#badgeenabledchangedcallback12) | 否 | 回调应用角标使能状态变化。 | 456 457**示例:** 458 459```ts 460import { BusinessError } from '@kit.BasicServicesKit'; 461 462let subscribeCallback = (err: BusinessError) => { 463 if (err) { 464 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 465 } else { 466 console.info('subscribeCallback'); 467 } 468}; 469 470let BadgeEnabledChangedCallback = (data: notificationSubscribe.EnabledNotificationCallbackData) => { 471 console.info('onBadgeEnabledChanged, badge enabled state change to: ', JSON.stringify(data)); 472}; 473let subscriber: notificationSubscribe.NotificationSubscriber = { 474 onBadgeEnabledChanged: BadgeEnabledChangedCallback 475}; 476 477notificationSubscribe.subscribe(subscriber, subscribeCallback); 478``` 479 480 481## onBatchCancel<sup>11+</sup> 482 483onBatchCancel?: (data: Array<SubscribeCallbackData\>) => void 484 485批量删除的回调函数。 486 487**系统能力**:SystemCapability.Notification.Notification 488 489**系统接口**: 此接口为系统接口。 490 491**参数:** 492 493| 参数名 | 类型 | 必填 | 说明 | 494| -------- | ------------------------------------------------------------ | ---- | -------------------------- | 495| onBatchCancel | (data: Array<[SubscribeCallbackData](#subscribecallbackdata)>) => void | 否 | 批量删除的通知信息。 | 496 497**示例:** 498 499```ts 500import { BusinessError } from '@kit.BasicServicesKit'; 501 502let subscribeCallback = (err: BusinessError) => { 503 if (err) { 504 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 505 } else { 506 console.info("subscribeCallback"); 507 } 508}; 509 510let onBatchCancelCallBack = (data: Array<notificationSubscribe.SubscribeCallbackData>) => { 511 console.info('===> onBatchCancel in test'); 512 let req = data[0].request; 513 console.info('===> onBatchCancel callback req.id:' + req.id); 514}; 515 516let subscriber: notificationSubscribe.NotificationSubscriber = { 517 onBatchCancel: onBatchCancelCallBack 518}; 519 520notificationSubscribe.subscribe(subscriber, subscribeCallback); 521``` 522## SubscribeCallbackData 523 524**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification 525 526**系统接口**:此接口为系统接口。 527 528| 名称 | 类型 | 可读 | 可写 | 说明 | 529| --------------- |--------------------------------------------------------------------| ---- | --- | -------- | 530| request | [NotificationRequest](js-apis-inner-notification-notificationRequest-sys.md#notificationrequest) | 是 | 否 | 通知内容。 | 531| sortingMap | [NotificationSortingMap](js-apis-inner-notification-notificationSortingMap-sys.md) | 是 | 否 | 通知排序信息。 | 532| reason | number | 是 | 否 | 删除原因(1:点击通知后删除通知,2:用户删除通知) 。| 533| sound | string | 是 | 否 | 通知声音。 | 534| vibrationValues | Array\<number\> | 是 | 否 | 通知震动。 | 535 536 537## EnabledNotificationCallbackData<sup>8+</sup> 538 539**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification 540 541**系统接口**:此接口为系统接口。 542 543| 名称 | 类型 | 可读 | 可写 | 说明 | 544| ------ | ------- | ---- | --- | ---------------- | 545| bundle | string | 是 | 否 | 应用的包名。 | 546| uid | number | 是 | 否 | 应用的uid。 | 547| enable | boolean | 是 | 否 | 应用通知使能状态。 | 548 549 550## BadgeNumberCallbackData<sup>10+</sup> 551 552**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification 553 554**系统接口**:此接口为系统接口。 555 556| 名称 | 类型 | 可读 | 可写 | 说明 | 557| ----------- | ------ | ---- | ---- | ------------ | 558| bundle | string | 是 | 否 | 应用的包名。 | 559| uid | number | 是 | 否 | 应用的uid。 | 560| badgeNumber | number | 是 | 否 | 角标个数。 | 561| instanceKey | number | 是 | 否 | 应用实例键值。 | 562 563 564## BadgeEnabledChangedCallback<sup>12+</sup> 565 566**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification 567 568**系统接口**:此接口为系统接口。 569 570| 名称 | 类型 | 只读 | 可选 | 说明 | 571| ----------- | ------ | ---- | ---- |------------ | 572| data | [EnabledNotificationCallbackData](#enablednotificationcallbackdata8) | 是 | 是 | 回调返回监听到的角标使能状态信息。 | 573 574