1# NotificationSubscriber 2 3The **NotificationSubscriber** module provides callbacks for receiving or removing notifications and serves as the input parameter of [subscribe](js-apis-notificationSubscribe.md). 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```js 12import notificationSubscribe from '@ohos.notificationSubscribe'; 13``` 14 15**System API**: This is a system API and cannot be called by third-party applications. 16 17### onConsume 18 19onConsume?: (data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void 20 21Called when a new notification is received. 22 23**System capability**: SystemCapability.Notification.Notification 24 25**System API**: This is a system API and cannot be called by third-party applications. 26 27**Parameters** 28 29| Name| Type| Mandatory| Description| 30| ------------ | ------------------------ | ---- | -------------------------- | 31| data | [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) | Yes| Information about the notification received.| 32 33**Example** 34 35```ts 36import Base from '@ohos.base'; 37 38let subscribeCallback = (err: Base.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](js-apis-notification.md#subscribecallbackdata)) => void 62 63Called when a notification is canceled. 64 65**System capability**: SystemCapability.Notification.Notification 66 67**System API**: This is a system API and cannot be called by third-party applications. 68 69**Parameters** 70 71| Name| Type| Mandatory| Description| 72| ------------ | ------------------------ | ---- | -------------------------- | 73| data | [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) | Yes| Information about the notification to cancel.| 74 75**Example** 76 77```ts 78import Base from '@ohos.base'; 79 80let subscribeCallback = (err: Base.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 88function 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](js-apis-notification.md#notificationsortingmap)) => void 104 105Called when notification sorting is updated. 106 107**System capability**: SystemCapability.Notification.Notification 108 109**System API**: This is a system API and cannot be called by third-party applications. 110 111**Parameters** 112 113| Name| Type| Mandatory| Description| 114| ------------ | ------------------------ | ---- | -------------------------- | 115| data | [NotificationSortingMap](js-apis-notification.md#notificationsortingmap) | Yes| Latest notification sorting list.| 116 117**Example** 118 119```ts 120import Base from '@ohos.base'; 121 122let subscribeCallback = (err: Base.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 143Called when the subscription is complete. 144 145**System capability**: SystemCapability.Notification.Notification 146 147**System API**: This is a system API and cannot be called by third-party applications. 148 149**Example** 150 151```ts 152import Base from '@ohos.base'; 153 154let subscribeCallback = (err: Base.BusinessError) => { 155 if (err) { 156 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 157 } else { 158 console.info("subscribeCallback"); 159 } 160}; 161 162let onConnectCallback = () => { 163 console.info('===> onConnect in test'); 164} 165 166let subscriber: notificationSubscribe.NotificationSubscriber = { 167 onConnect: onConnectCallback 168}; 169 170notificationSubscribe.subscribe(subscriber, subscribeCallback); 171``` 172 173### onDisconnect 174 175onDisconnect?:() => void 176 177Called when unsubscription is complete. 178 179**System capability**: SystemCapability.Notification.Notification 180 181**System API**: This is a system API and cannot be called by third-party applications. 182 183**Example** 184 185```ts 186import Base from '@ohos.base'; 187 188let subscribeCallback = (err: Base.BusinessError) => { 189 if (err) { 190 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 191 } else { 192 console.info("subscribeCallback"); 193 } 194}; 195let unsubscribeCallback = (err: Base.BusinessError) => { 196 if (err) { 197 console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`); 198 } else { 199 console.info("unsubscribeCallback"); 200 } 201}; 202 203let onConnectCallback = () => { 204 console.info('===> onConnect in test'); 205} 206let onDisconnectCallback = () => { 207 console.info('===> onDisconnect in test'); 208} 209 210let subscriber: notificationSubscribe.NotificationSubscriber = { 211 onConnect: onConnectCallback, 212 onDisconnect: onDisconnectCallback 213}; 214 215// The onConnect callback is invoked when subscription to the notification is complete. 216notificationSubscribe.subscribe(subscriber, subscribeCallback); 217// The onDisconnect callback is invoked when unsubscription to the notification is complete. 218notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback); 219``` 220 221### onDestroy 222 223onDestroy?:() => void 224 225Called when the service is disconnected. 226 227**System capability**: SystemCapability.Notification.Notification 228 229**System API**: This is a system API and cannot be called by third-party applications. 230 231**Example** 232 233```ts 234import Base from '@ohos.base'; 235 236let subscribeCallback = (err: Base.BusinessError) => { 237 if (err) { 238 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 239 } else { 240 console.info("subscribeCallback"); 241 } 242}; 243 244let onDestroyCallback = () => { 245 console.info('===> onDestroy in test'); 246} 247 248let subscriber: notificationSubscribe.NotificationSubscriber = { 249 onDestroy: onDestroyCallback 250}; 251 252notificationSubscribe.subscribe(subscriber, subscribeCallback); 253``` 254 255### onDoNotDisturbDateChange<sup>8+</sup> 256 257onDoNotDisturbDateChange?:(mode: notification.[DoNotDisturbDate](js-apis-notificationManager.md#donotdisturbdate)) => void 258 259Called when the DND time settings are changed. 260 261**System capability**: SystemCapability.Notification.Notification 262 263**System API**: This is a system API and cannot be called by third-party applications. 264 265**Parameters** 266 267| Name| Type| Mandatory| Description| 268| ------------ | ------------------------ | ---- | -------------------------- | 269| mode | notification.[DoNotDisturbDate](js-apis-notificationManager.md#DoNotDisturbDate) | Yes| DND time setting updates.| 270 271**Example** 272 273```ts 274import Base from '@ohos.base'; 275 276let subscribeCallback = (err: Base.BusinessError) => { 277 if (err) { 278 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 279 } else { 280 console.info("subscribeCallback"); 281 } 282}; 283 284let onDoNotDisturbDateChangeCallback = (mode: NotificationManager.DoNotDisturbDate) => { 285 console.info('===> onDoNotDisturbDateChange:' + mode); 286} 287 288let subscriber: notificationSubscribe.NotificationSubscriber = { 289 onDoNotDisturbDateChange: onDoNotDisturbDateChangeCallback 290}; 291 292notificationSubscribe.subscribe(subscriber, subscribeCallback); 293``` 294 295 296### onEnabledNotificationChanged<sup>8+</sup> 297 298onEnabledNotificationChanged?:(callbackData: [EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata)) => void 299 300Listens for the notification enabled status changes. 301 302**System capability**: SystemCapability.Notification.Notification 303 304**System API**: This is a system API and cannot be called by third-party applications. 305 306**Parameters** 307 308| Name| Type| Mandatory| Description| 309| ------------ | ------------------------ | ---- | -------------------------- | 310| callback | AsyncCallback\<[EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata)\> | Yes| Callback used to return the result.| 311 312**Example** 313 314```ts 315import Base from '@ohos.base'; 316 317let subscribeCallback = (err: Base.BusinessError) => { 318 if (err) { 319 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 320 } else { 321 console.info("subscribeCallback"); 322 } 323}; 324 325let onEnabledNotificationChangedCallback = (callbackData: notificationSubscribe.EnabledNotificationCallbackData) => { 326 console.info("bundle: ", callbackData.bundle); 327 console.info("uid: ", callbackData.uid); 328 console.info("enable: ", callbackData.enable); 329}; 330 331let subscriber: notificationSubscribe.NotificationSubscriber = { 332 onEnabledNotificationChanged: onEnabledNotificationChangedCallback 333}; 334 335notificationSubscribe.subscribe(subscriber, subscribeCallback); 336``` 337 338### onBadgeChanged<sup>10+</sup> 339 340 onBadgeChanged?:(data: [BadgeNumberCallbackData](#badgenumbercallbackdata10)) => void 341 342Listens for the change of the notification badge number. 343 344**System capability**: SystemCapability.Notification.Notification 345 346**System API**: This is a system API and cannot be called by third-party applications. 347 348**Parameters** 349 350| Name | Type | Mandatory| Description | 351| -------- | ------------------------------------------------------------ | ---- | -------------------------- | 352| callback | AsyncCallback\<[BadgeNumberCallbackData](#badgenumbercallbackdata10)\> | Yes | Callback used to return the result.| 353 354**Example** 355 356```ts 357import Base from '@ohos.base'; 358 359let subscribeCallback = (err: Base.BusinessError) => { 360 if (err) { 361 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 362 } else { 363 console.info("subscribeCallback"); 364 } 365}; 366 367let subscriber: notificationSubscribe.NotificationSubscriber = { 368 onBadgeChanged: (data) => { 369 console.info("bundle: ", data.bundle); 370 console.info("uid: ", data.uid); 371 console.info("badgeNumber: ", data.badgeNumber); 372 } 373}; 374 375notificationSubscribe.subscribe(subscriber, subscribeCallback); 376``` 377 378## SubscribeCallbackData 379 380**System capability**: SystemCapability.Notification.Notification 381 382**System API**: This is a system API and cannot be called by third-party applications. 383 384| Name | Type | Readable| Writable| Description | 385| --------------- | ------------------------------------------------- | ---- | --- | -------- | 386| request | [NotificationRequest](js-apis-inner-notification-notificationRequest#notificationrequest) | Yes | No | Notification content.| 387| sortingMap | [NotificationSortingMap](js-apis-inner-notification-notificationSortingMap.md) | Yes | No | Notification sorting information.| 388| reason | number | Yes | No | Reason for deletion.| 389| sound | string | Yes | No | Sound used for notification.| 390| vibrationValues | Array\<number\> | Yes | No | Vibration used for notification.| 391 392 393## EnabledNotificationCallbackData<sup>8+</sup> 394 395**System capability**: SystemCapability.Notification.Notification 396 397**System API**: This is a system API and cannot be called by third-party applications. 398 399| Name | Type | Readable| Writable| Description | 400| ------ | ------- | ---- | --- | ---------------- | 401| bundle | string | Yes | No | Bundle name of the application. | 402| uid | number | Yes | No | UID of the application. | 403| enable | boolean | Yes | No | Notification enabled status of the application.| 404 405 406## BadgeNumberCallbackData<sup>10+</sup> 407 408**System capability**: SystemCapability.Notification.Notification 409 410**System API**: This is a system API and cannot be called by third-party applications. 411 412| Name | Type | Readable| Writable| Description | 413| ----------- | ------ | ---- | ---- | ------------ | 414| bundle | string | Yes | No | Bundle name of the application.| 415| uid | number | Yes | No | UID of the application. | 416| badgeNumber | number | Yes | No | Number of notifications displayed on the application icon. | 417