js-apis-notificationSubscribe.md 33.4 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.notificationSubscribe (NotificationSubscribe)
2

E
ester.zhou 已提交
3
The **notificationSubscribe** module provides APIs for notification subscription, notification unsubscription, subscription removal, and more. In general cases, only system applications can call these APIs.
4 5 6 7 8 9 10 11

> **NOTE**
>
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

```js
E
ester.zhou 已提交
12
import notificationSubscribe from '@ohos.notificationSubscribe';
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
```



## NotificationSubscribe.subscribe

subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback\<void\>): void

Subscribes to a notification with the subscription information specified. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name      | Type                     | Mandatory| Description            |
| ---------- | ------------------------- | ---- | ---------------- |
E
ester.zhou 已提交
33 34
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber)    | Yes  | Notification subscriber.    |
| info       | [NotificationSubscribeInfo](js-apis-notification.md#notificationsubscribeinfo) | Yes  | Notification subscription information.|
35 36 37 38
| callback   | AsyncCallback\<void\>     | Yes  | Callback used to return the result.|

**Error codes**

E
ester.zhou 已提交
39 40
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

41 42
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
43 44 45
| 201  | Permission denied.                      |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.             |
46 47 48 49 50 51 52 53 54 55
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
// subscribe callback
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
56
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
57 58 59 60 61 62 63
    } else {
        console.info("subscribe success");
    }
}
function onConsumeCallback(data) {
	console.info("Consume callback: " + JSON.stringify(data));
}
E
ester.zhou 已提交
64
let subscriber = {
65
    onConsume: onConsumeCallback
E
ester.zhou 已提交
66 67
};
let info = {
68
    bundleNames: ["bundleName1","bundleName2"]
E
ester.zhou 已提交
69
};
E
ester.zhou 已提交
70
notificationSubscribe.subscribe(subscriber, info, subscribeCallback);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
```

## NotificationSubscribe.subscribe

subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void

Subscribes to notifications of all applications under this user. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name      | Type                  | Mandatory| Description            |
| ---------- | ---------------------- | ---- | ---------------- |
E
ester.zhou 已提交
89
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes  | Notification subscriber.    |
90 91 92 93
| callback   | AsyncCallback\<void\>  | Yes  | Callback used to return the result.|

**Error codes**

E
ester.zhou 已提交
94 95
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

96 97
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
98 99 100
| 201  | Permission denied.                      |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.             |
101 102 103 104 105 106 107 108 109
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
110
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
111 112 113 114 115 116 117
    } else {
        console.info("subscribe success");
    }
}
function onConsumeCallback(data) {
	console.info("Consume callback: " + JSON.stringify(data));
}
E
ester.zhou 已提交
118
let subscriber = {
119
    onConsume: onConsumeCallback
E
ester.zhou 已提交
120
};
E
ester.zhou 已提交
121
notificationSubscribe.subscribe(subscriber, subscribeCallback);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
```



## NotificationSubscribe.subscribe

subscribe(subscriber: NotificationSubscriber, info?: NotificationSubscribeInfo): Promise\<void\>

Subscribes to a notification with the subscription information specified. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name      | Type                     | Mandatory| Description        |
| ---------- | ------------------------- | ---- | ------------ |
E
ester.zhou 已提交
142 143
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber)    | Yes  | Notification subscriber.|
| info       | [NotificationSubscribeInfo](js-apis-notification.md#notificationsubscribeinfo) | No  | Notification subscription information.  |
144 145 146

**Error codes**

E
ester.zhou 已提交
147 148
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

149 150
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
151 152 153
| 201  | Permission denied.                      |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.             |
154 155 156 157 158 159 160 161 162 163
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
function onConsumeCallback(data) {
    console.info("Consume callback: " + JSON.stringify(data));
}
E
ester.zhou 已提交
164
let subscriber = {
165 166
    onConsume: onConsumeCallback
};
E
ester.zhou 已提交
167
notificationSubscribe.subscribe(subscriber).then(() => {
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	console.info("subscribe success");
});
```



## NotificationSubscribe.unsubscribe

unsubscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void

Unsubscribes from a notification. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name      | Type                  | Mandatory| Description                |
| ---------- | ---------------------- | ---- | -------------------- |
E
ester.zhou 已提交
190
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes  | Notification subscriber.        |
191 192 193 194
| callback   | AsyncCallback\<void\>  | Yes  | Callback used to return the result.|

**Error codes**

E
ester.zhou 已提交
195 196
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

197 198
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
199 200 201
| 201  | Permission denied.                      |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.             |
202 203 204 205 206 207 208 209 210
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
function unsubscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
211
        console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
212 213 214 215
    } else {
        console.info("unsubscribe success");
    }
}
E
ester.zhou 已提交
216 217
function onDisconnectCallback() {
	console.info("subscribe disconnect");
218
}
E
ester.zhou 已提交
219
let subscriber = {
220
    onDisconnect: onDisconnectCallback
E
ester.zhou 已提交
221
};
E
ester.zhou 已提交
222
notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
```

## NotificationSubscribe.unsubscribe

unsubscribe(subscriber: NotificationSubscriber): Promise\<void\>

Unsubscribes from a notification. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name      | Type                  | Mandatory| Description        |
| ---------- | ---------------------- | ---- | ------------ |
E
ester.zhou 已提交
241
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes  | Notification subscriber.|
242 243 244

**Error codes**

E
ester.zhou 已提交
245 246
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

247 248
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
249 250 251
| 201  | Permission denied.                      |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.             |
252 253 254 255 256 257 258
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
E
ester.zhou 已提交
259 260
function onDisconnectCallback() {
	console.info("subscribe disconnect");
261
}
E
ester.zhou 已提交
262
let subscriber = {
263 264
    onDisconnect: onDisconnectCallback
};
E
ester.zhou 已提交
265
notificationSubscribe.unsubscribe(subscriber).then(() => {
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	console.info("unsubscribe success");
});
```

## NotificationSubscribe.remove

remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason, callback: AsyncCallback\<void\>): void

Removes a notification for a specified application. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name           | Type                               | Mandatory| Description                |
| --------------- |   ----------------------------------| ---- | -------------------- |
E
ester.zhou 已提交
286 287
| bundle          | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)       | Yes  | Bundle information of the application.          |
| notificationKey | [NotificationKey](js-apis-notification.md#notificationkey) | Yes  | Notification key.            |
288 289 290 291 292
| reason          | [RemoveReason](#removereason)      | Yes  | Reason for removing the notification.        |
| callback        | AsyncCallback\<void\>               | Yes  | Callback used to return the result.|

**Error codes**

E
ester.zhou 已提交
293 294
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

295 296
| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
297 298 299
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
300 301 302 303 304 305 306 307 308 309 310
| 1600001  | Internal error.                          |
| 1600002  | Marshalling or unmarshalling error.      |
| 1600003  | Failed to connect service.               |
| 1600007  | The notification is not exist.           |
| 17700001 | The specified bundle name was not found. |

**Example**

```js
function removeCallback(err) {
    if (err) {
E
ester.zhou 已提交
311
        console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
312 313 314 315
    } else {
        console.info("remove success");
    }
}
E
ester.zhou 已提交
316
let bundle = {
317
    bundle: "bundleName1",
E
ester.zhou 已提交
318 319
};
let notificationKey = {
320 321
    id: 0,
    label: "label",
E
ester.zhou 已提交
322
};
E
ester.zhou 已提交
323 324
let reason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(bundle, notificationKey, reason, removeCallback);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
```



## NotificationSubscribe.remove

remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason): Promise\<void\>

Removes a notification for a specified application. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name           | Type           | Mandatory| Description      |
| --------------- | --------------- | ---- | ---------- |
E
ester.zhou 已提交
345
| bundle          | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)    | Yes  | Bundle information of the application.|
E
ester.zhou 已提交
346
| notificationKey | [NotificationKey](js-apis-notification.md#notificationkey) | Yes  | Notification key.  |
347 348 349 350
| reason          | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.        |

**Error codes**

E
ester.zhou 已提交
351 352
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

353 354
| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
355 356 357
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
358 359 360 361 362 363 364 365 366
| 1600001  | Internal error.                          |
| 1600002  | Marshalling or unmarshalling error.      |
| 1600003  | Failed to connect service.               |
| 1600007  | The notification is not exist.           |
| 17700001 | The specified bundle name was not found. |

**Example**

```js
E
ester.zhou 已提交
367
let bundle = {
368
    bundle: "bundleName1",
E
ester.zhou 已提交
369 370
};
let notificationKey = {
371 372
    id: 0,
    label: "label",
E
ester.zhou 已提交
373 374
};
let reason = NotificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
E
ester.zhou 已提交
375
notificationSubscribe.remove(bundle, notificationKey, reason).then(() => {
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	console.info("remove success");
});
```

## NotificationSubscribe.remove

remove(hashCode: string, reason: RemoveReason, callback: AsyncCallback\<void\>): void

Removes a specified notification. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name    | Type                 | Mandatory| Description                |
| -------- | --------------------- | ---- | -------------------- |
E
ester.zhou 已提交
396
| hashCode | string                | Yes  | Unique notification ID. It is the value of **hashCode** in the [NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest) object of [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) in the [onConsume](#onconsume) callback. |
397 398 399 400 401
| reason   | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.        |
| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|

**Error codes**

E
ester.zhou 已提交
402 403
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

404 405
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
406 407 408
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
409 410 411 412 413 414 415 416
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600007  | The notification is not exist.      |

**Example**

```js
E
ester.zhou 已提交
417
let hashCode = 'hashCode';
418 419 420

function removeCallback(err) {
    if (err) {
E
ester.zhou 已提交
421
        console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
422 423 424 425
    } else {
        console.info("remove success");
    }
}
E
ester.zhou 已提交
426
let reason = NotificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
E
ester.zhou 已提交
427
notificationSubscribe.remove(hashCode, reason, removeCallback);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
```

## NotificationSubscribe.remove

remove(hashCode: string, reason: RemoveReason): Promise\<void\>

Removes a specified notification. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name    | Type      | Mandatory| Description      |
| -------- | ---------- | ---- | ---------- |
| hashCode | string | Yes  | Unique notification ID.|
| reason   | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.        |

**Error codes**

E
ester.zhou 已提交
451 452
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

453 454
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
455 456 457
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
458 459 460 461 462 463 464 465
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600007  | The notification is not exist.      |

**Example**

```js
E
ester.zhou 已提交
466
let hashCode = 'hashCode';
E
ester.zhou 已提交
467 468
let reason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(hashCode, reason).then(() => {
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	console.info("remove success");
});
```

## NotificationSubscribe.removeAll

removeAll(bundle: BundleOption, callback: AsyncCallback\<void\>): void

Removes all notifications for a specified application. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**Parameters**

| Name    | Type                 | Mandatory| Description                        |
| -------- | --------------------- | ---- | ---------------------------- |
E
ester.zhou 已提交
489
| bundle   | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)          | Yes  | Bundle information of the application.                  |
490 491 492 493
| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|

**Error codes**

E
ester.zhou 已提交
494 495
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

496 497
| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
498 499 500
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
501 502 503 504 505 506 507 508 509 510
| 1600001  | Internal error.                          |
| 1600002  | Marshalling or unmarshalling error.      |
| 1600003  | Failed to connect service.               |
| 17700001 | The specified bundle name was not found. |

**Example**

```js
function removeAllCallback(err) {
    if (err) {
E
ester.zhou 已提交
511
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
512 513 514 515
    } else {
        console.info("removeAll success");
    }
}
E
ester.zhou 已提交
516
let bundle = {
517
    bundle: "bundleName1",
E
ester.zhou 已提交
518
};
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
NotificationSubscribe.removeAll(bundle, removeAllCallback);
```

## NotificationSubscribe.removeAll

removeAll(callback: AsyncCallback\<void\>): void

Removes all notifications. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name    | Type                 | Mandatory| Description                |
| -------- | --------------------- | ---- | -------------------- |
| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|

**Error codes**

E
ester.zhou 已提交
542 543
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

544 545
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
546 547 548
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
549 550 551 552 553 554 555 556 557
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
function removeAllCallback(err) {
    if (err) {
E
ester.zhou 已提交
558
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
559 560 561 562 563
    } else {
        console.info("removeAll success");
    }
}

E
ester.zhou 已提交
564
notificationSubscribe.removeAll(removeAllCallback);
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
```

## NotificationSubscribe.removeAll

removeAll(bundle?: BundleOption): Promise\<void\>

Removes all notifications for a specified application. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name  | Type        | Mandatory| Description      |
| ------ | ------------ | ---- | ---------- |
E
ester.zhou 已提交
583
| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | No  | Bundle information of the application.|
584 585 586

**Error codes**

E
ester.zhou 已提交
587 588
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

589 590
| ID| Error Message                                |
| -------- | ---------------------------------------- |
E
ester.zhou 已提交
591 592 593
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
594 595 596 597 598 599 600 601 602
| 1600001  | Internal error.                          |
| 1600002  | Marshalling or unmarshalling error.      |
| 1600003  | Failed to connect service.               |
| 17700001 | The specified bundle name was not found. |

**Example**

```js
// If no application is specified, notifications of all applications are deleted.
E
ester.zhou 已提交
603
notificationSubscribe.removeAll().then(() => {
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
	console.info("removeAll success");
});
```

## NotificationSubscribe.removeAll

removeAll(userId: number, callback: AsyncCallback\<void>): void

Removes all notifications for a specified user. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name  | Type        | Mandatory| Description      |
| ------ | ------------ | ---- | ---------- |
| userId | number | Yes  | User ID.|
| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|

**Error codes**

E
ester.zhou 已提交
629 630
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

631 632
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
633 634 635
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
636 637 638 639 640 641 642 643 644 645
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600008  | The user is not exist.              |

**Example**

```js
function removeAllCallback(err) {
    if (err) {
E
ester.zhou 已提交
646
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
647 648 649 650 651
    } else {
        console.info("removeAll success");
    }
}

E
ester.zhou 已提交
652
let userId = 1;
653

E
ester.zhou 已提交
654
notificationSubscribe.removeAll(userId, removeAllCallback);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
```

## Notification.removeAll

removeAll(userId: number): Promise\<void>

Removes all notifications for a specified user. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.Notification

**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name  | Type        | Mandatory| Description      |
| ------ | ------------ | ---- | ---------- |
| userId | number | Yes  | User ID.|

**Error codes**

E
ester.zhou 已提交
677 678
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

679 680
| ID| Error Message                           |
| -------- | ----------------------------------- |
E
ester.zhou 已提交
681 682 683
| 201  | Permission denied.                           |
| 202  | Not system application to call the interface. |
| 401  | The parameter check failed.                  |
684 685 686 687 688 689 690 691 692 693
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600008  | The user is not exist.              |

**Example**

```js
function removeAllCallback(err) {
    if (err) {
E
ester.zhou 已提交
694
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
695 696 697 698 699
    } else {
        console.info("removeAll success");
    }
}

E
ester.zhou 已提交
700
let userId = 1;
701

E
ester.zhou 已提交
702
notificationSubscribe.removeAll(userId, removeAllCallback);
703 704 705 706
```

## NotificationSubscriber

E
ester.zhou 已提交
707
Provides callbacks for receiving or removing notifications and serves as the input parameter of [subscribe](#notificationsubscribesubscribe).
708 709 710 711 712

**System API**: This is a system API and cannot be called by third-party applications.

### onConsume

E
ester.zhou 已提交
713
onConsume?: (data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void
714

E
ester.zhou 已提交
715
Called when a notification is received.
716 717 718 719 720 721 722 723 724

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| ------------ | ------------------------ | ---- | -------------------------- |
E
ester.zhou 已提交
725
| data | [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) | Yes| Information about the notification received.|
726 727 728 729 730 731

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
732
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
733 734 735 736 737 738 739 740 741 742 743
    } else {
        console.info("subscribeCallback");
    }
};

function onConsumeCallback(data) {
    console.info('===> onConsume in test');
    let req = data.request;
    console.info('===> onConsume callback req.id:' + req.id);
};

E
ester.zhou 已提交
744
let subscriber = {
745 746 747
    onConsume: onConsumeCallback
};

E
ester.zhou 已提交
748
notificationSubscribe.subscribe(subscriber, subscribeCallback);
749 750 751 752
```

### onCancel

E
ester.zhou 已提交
753
onCancel?:(data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void
754

E
ester.zhou 已提交
755
Called when a notification is canceled.
756 757 758 759 760 761 762 763 764

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| ------------ | ------------------------ | ---- | -------------------------- |
E
ester.zhou 已提交
765
| data | [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) | Yes| Information about the notification to cancel.|
766 767 768 769 770 771

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
772
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
773 774 775 776 777 778 779 780 781 782 783
    } else {
        console.info("subscribeCallback");
    }
};

function onCancelCallback(data) {
    console.info('===> onCancel in test');
    let req = data.request;
    console.info('===> onCancel callback req.id:' + req.id);
}

E
ester.zhou 已提交
784
let subscriber = {
785 786 787
    onCancel: onCancelCallback
};

E
ester.zhou 已提交
788
notificationSubscribe.subscribe(subscriber, subscribeCallback);
789 790 791 792
```

### onUpdate

E
ester.zhou 已提交
793
onUpdate?:(data: [NotificationSortingMap](js-apis-notification.md#notificationsortingmap)) => void
794

E
ester.zhou 已提交
795
Called when the notification sorting list is updated.
796 797 798 799 800 801 802 803 804

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| ------------ | ------------------------ | ---- | -------------------------- |
E
ester.zhou 已提交
805
| data | [NotificationSortingMap](js-apis-notification.md#notificationsortingmap)) | Yes| Latest notification sorting list.|
806 807 808 809 810 811

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
812
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
813 814 815 816 817 818 819 820 821
    } else {
        console.info("subscribeCallback");
    }
};

function onUpdateCallback(map) {
    console.info('===> onUpdateCallback map:' + JSON.stringify(map));
}

E
ester.zhou 已提交
822
let subscriber = {
823 824 825
    onUpdate: onUpdateCallback
};

E
ester.zhou 已提交
826
notificationSubscribe.subscribe(subscriber, subscribeCallback);
827 828 829 830 831 832
```

### onConnect

onConnect?:() => void

E
ester.zhou 已提交
833
Called when subscription is complete.
834 835 836 837 838 839 840 841 842 843

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
844
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
845 846 847 848 849 850 851 852 853
    } else {
        console.info("subscribeCallback");
    }
};

function onConnectCallback() {
    console.info('===> onConnect in test');
}

E
ester.zhou 已提交
854
let subscriber = {
855 856 857
    onConnect: onConnectCallback
};

E
ester.zhou 已提交
858
notificationSubscribe.subscribe(subscriber, subscribeCallback);
859 860 861 862 863 864
```

### onDisconnect

onDisconnect?:() => void

E
ester.zhou 已提交
865
Called when unsubscription is complete.
866 867 868 869 870 871 872 873 874 875

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
876
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
877 878 879 880 881 882
    } else {
        console.info("subscribeCallback");
    }
};
function unsubscribeCallback(err) {
    if (err.code) {
E
ester.zhou 已提交
883
        console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
884 885 886 887 888 889 890 891 892 893 894 895
    } else {
        console.info("unsubscribeCallback");
    }
};

function onConnectCallback() {
    console.info('===> onConnect in test');
}
function onDisconnectCallback() {
    console.info('===> onDisconnect in test');
}

E
ester.zhou 已提交
896
let subscriber = {
897 898 899 900 901
    onConnect: onConnectCallback,
    onDisconnect: onDisconnectCallback
};

// The onConnect callback is invoked when subscription to the notification is complete.
E
ester.zhou 已提交
902
notificationSubscribe.subscribe(subscriber, subscribeCallback);
903
// The onDisconnect callback is invoked when unsubscription to the notification is complete.
E
ester.zhou 已提交
904
notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
```

### onDestroy

onDestroy?:() => void

Callback for service disconnection.

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
922
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
923 924 925 926 927 928 929 930 931
    } else {
        console.info("subscribeCallback");
    }
};

function onDestroyCallback() {
    console.info('===> onDestroy in test');
}

E
ester.zhou 已提交
932
let subscriber = {
933 934 935
    onDestroy: onDestroyCallback
};

E
ester.zhou 已提交
936
notificationSubscribe.subscribe(subscriber, subscribeCallback);
937 938 939 940 941 942
```

### onDoNotDisturbDateChange

onDoNotDisturbDateChange?:(mode: notification.[DoNotDisturbDate](js-apis-notificationManager.md#donotdisturbdate)) => void

E
ester.zhou 已提交
943
Called when the DND time setting is updated.
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| ------------ | ------------------------ | ---- | -------------------------- |
| mode | notification.[DoNotDisturbDate](js-apis-notificationManager.md#DoNotDisturbDate) | Yes| DND time setting updates.|

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
960
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
961 962 963 964 965 966 967 968 969
    } else {
        console.info("subscribeCallback");
    }
};

function onDoNotDisturbDateChangeCallback(mode) {
    console.info('===> onDoNotDisturbDateChange:' + mode);
}

E
ester.zhou 已提交
970
let subscriber = {
971 972 973
    onDoNotDisturbDateChange: onDoNotDisturbDateChangeCallback
};

E
ester.zhou 已提交
974
notificationSubscribe.subscribe(subscriber, subscribeCallback);
975 976 977 978 979
```


### onEnabledNotificationChanged

E
ester.zhou 已提交
980
onEnabledNotificationChanged?:(callbackData: [EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata8)) => void
981 982 983 984 985 986 987 988 989 990 991

Listens for the notification enabled status changes. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| ------------ | ------------------------ | ---- | -------------------------- |
E
ester.zhou 已提交
992
| callback | AsyncCallback\<[EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata8)\> | Yes| Callback used to return the result.|
993 994 995 996 997 998

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
999
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
    } else {
        console.info("subscribeCallback");
    }
};

function onEnabledNotificationChangedCallback(callbackData) {
    console.info("bundle: ", callbackData.bundle);
    console.info("uid: ", callbackData.uid);
    console.info("enable: ", callbackData.enable);
};

E
ester.zhou 已提交
1011
let subscriber = {
1012 1013 1014
    onEnabledNotificationChanged: onEnabledNotificationChangedCallback
};

E
ester.zhou 已提交
1015
notificationSubscribe.subscribe(subscriber, subscribeCallback);
1016 1017
```

E
ester.zhou 已提交
1018
## RemoveReason
1019 1020 1021 1022 1023

**System capability**: SystemCapability.Notification.Notification

**System API**: This is a system API and cannot be called by third-party applications.

E
ester.zhou 已提交
1024 1025 1026 1027
| Name                | Value | Description                 |
| -------------------- | --- | -------------------- |
| CLICK_REASON_REMOVE  | 1   | The notification is removed after a click on it.   |
| CANCEL_REASON_REMOVE | 2   | The notification is removed by the user.        |