js-apis-notificationSubscribe.md 37.0 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.notificationSubscribe (NotificationSubscribe)
E
esterzhou 已提交
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.
E
esterzhou 已提交
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';
E
esterzhou 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
```



## 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            |
| ---------- | ------------------------- | ---- | ---------------- |
| subscriber | [NotificationSubscriber](#notificationsubscriber)    | Yes  | Notification subscriber.    |
| info       | [NotificationSubscribeInfo](#notificationsubscribeinfo) | Yes  | Notification subscription information.|
| 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).

E
esterzhou 已提交
41 42 43 44 45 46 47 48 49 50 51 52
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 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 已提交
53
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
54 55 56 57 58 59 60
    } else {
        console.info("subscribe success");
    }
}
function onConsumeCallback(data) {
	console.info("Consume callback: " + JSON.stringify(data));
}
E
ester.zhou 已提交
61
let subscriber = {
E
esterzhou 已提交
62
    onConsume: onConsumeCallback
E
ester.zhou 已提交
63 64
};
let info = {
E
esterzhou 已提交
65
    bundleNames: ["bundleName1","bundleName2"]
E
ester.zhou 已提交
66 67
};
notificationSubscribe.subscribe(subscriber, info, subscribeCallback);
E
esterzhou 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
```

## 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            |
| ---------- | ---------------------- | ---- | ---------------- |
| subscriber | [NotificationSubscriber](#notificationsubscriber) | Yes  | Notification subscriber.    |
| callback   | AsyncCallback\<void\>  | Yes  | Callback used to return the result.|

**Error codes**

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

E
esterzhou 已提交
93 94 95 96 97 98 99 100 101 102 103
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

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



## 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        |
| ---------- | ------------------------- | ---- | ------------ |
| subscriber | [NotificationSubscriber](#notificationsubscriber)    | Yes  | Notification subscriber.|
| info       | [NotificationSubscribeInfo](#notificationsubscribeinfo) | No  | Notification subscription information.  |

**Error codes**

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

E
esterzhou 已提交
143 144 145 146 147 148 149 150 151 152 153 154
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 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 已提交
155
let subscriber = {
E
esterzhou 已提交
156 157
    onConsume: onConsumeCallback
};
E
ester.zhou 已提交
158
notificationSubscribe.subscribe(subscriber).then(() => {
E
esterzhou 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	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                |
| ---------- | ---------------------- | ---- | -------------------- |
| subscriber | [NotificationSubscriber](#notificationsubscriber) | Yes  | Notification subscriber.        |
| callback   | AsyncCallback\<void\>  | Yes  | Callback used to return the result.|

**Error codes**

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

E
esterzhou 已提交
188 189 190 191 192 193 194 195 196 197 198
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
function unsubscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
199
        console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
200 201 202 203 204 205 206
    } else {
        console.info("unsubscribe success");
    }
}
function onDisconnectCallback(data) {
	console.info("Cancel callback: " + JSON.stringify(data));
}
E
ester.zhou 已提交
207
let subscriber = {
E
esterzhou 已提交
208
    onDisconnect: onDisconnectCallback
E
ester.zhou 已提交
209 210
};
notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
E
esterzhou 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
```

## 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        |
| ---------- | ---------------------- | ---- | ------------ |
| subscriber | [NotificationSubscriber](#notificationsubscriber) | Yes  | Notification subscriber.|

**Error codes**

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

E
esterzhou 已提交
235 236 237 238 239 240 241 242 243 244 245 246
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
function onDisconnectCallback(data) {
	console.info("Cancel callback: " + JSON.stringify(data));
}
E
ester.zhou 已提交
247
let subscriber = {
E
esterzhou 已提交
248 249
    onDisconnect: onDisconnectCallback
};
E
ester.zhou 已提交
250
notificationSubscribe.unsubscribe(subscriber).then(() => {
E
esterzhou 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	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                |
| --------------- |   ----------------------------------| ---- | -------------------- |
| bundle          | [BundleOption](#bundleoption)       | Yes  | Bundle information of the application.          |
| notificationKey | [NotificationKey](#notificationkey) | Yes  | Notification key.            |
| reason          | [RemoveReason](#removereason)      | Yes  | Reason for removing the notification.        |
| callback        | AsyncCallback\<void\>               | Yes  | Callback used to return the result.|

**Error codes**

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

E
esterzhou 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292
| ID| Error Message                                |
| -------- | ---------------------------------------- |
| 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 已提交
293
        console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
294 295 296 297
    } else {
        console.info("remove success");
    }
}
E
ester.zhou 已提交
298
let bundle = {
E
esterzhou 已提交
299
    bundle: "bundleName1",
E
ester.zhou 已提交
300 301
};
let notificationKey = {
E
esterzhou 已提交
302 303
    id: 0,
    label: "label",
E
ester.zhou 已提交
304 305 306
};
let reason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(bundle, notificationKey, reason, removeCallback);
E
esterzhou 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
```



## 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      |
| --------------- | --------------- | ---- | ---------- |
| bundle          | [BundleOption](#bundleoption)    | Yes  | Bundle information of the application.|
| notificationKey | [NotificationKey](#notificationkey) | Yes  | Notification key.  |
| reason          | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.        |

**Error codes**

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

E
esterzhou 已提交
335 336 337 338 339 340 341 342 343 344 345
| ID| Error Message                                |
| -------- | ---------------------------------------- |
| 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 已提交
346
let bundle = {
E
esterzhou 已提交
347
    bundle: "bundleName1",
E
ester.zhou 已提交
348 349
};
let notificationKey = {
E
esterzhou 已提交
350 351
    id: 0,
    label: "label",
E
ester.zhou 已提交
352 353 354
};
let reason = NotificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(bundle, notificationKey, reason).then(() => {
E
esterzhou 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	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                |
| -------- | --------------------- | ---- | -------------------- |
| hashCode | string                | Yes  | Unique notification ID. It is the **hashCode** in the [NotificationRequest](#notificationrequest) object of [SubscribeCallbackData](#subscribecallbackdata) of the [onConsume](#onconsume) callback.|
| reason   | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.        |
| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|

**Error codes**

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

E
esterzhou 已提交
383 384 385 386 387 388 389 390 391 392
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600007  | The notification is not exist.      |

**Example**

```js
E
ester.zhou 已提交
393
let hashCode = 'hashCode';
E
esterzhou 已提交
394 395 396

function removeCallback(err) {
    if (err) {
E
ester.zhou 已提交
397
        console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
398 399 400 401
    } else {
        console.info("remove success");
    }
}
E
ester.zhou 已提交
402 403
let reason = NotificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
notificationSubscribe.remove(hashCode, reason, removeCallback);
E
esterzhou 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
```

## 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 已提交
427 428
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

E
esterzhou 已提交
429 430 431 432 433 434 435 436 437 438
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600007  | The notification is not exist.      |

**Example**

```js
E
ester.zhou 已提交
439 440 441
let hashCode = 'hashCode';
let reason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(hashCode, reason).then(() => {
E
esterzhou 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	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                        |
| -------- | --------------------- | ---- | ---------------------------- |
| bundle   | [BundleOption](#bundleoption)          | Yes  | Bundle information of the application.                  |
| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|

**Error codes**

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

E
esterzhou 已提交
469 470 471 472 473 474 475 476 477 478 479 480
| ID| Error Message                                |
| -------- | ---------------------------------------- |
| 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 已提交
481
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
482 483 484 485
    } else {
        console.info("removeAll success");
    }
}
E
ester.zhou 已提交
486
let bundle = {
E
esterzhou 已提交
487
    bundle: "bundleName1",
E
ester.zhou 已提交
488
};
E
esterzhou 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
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 已提交
512 513
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

E
esterzhou 已提交
514 515 516 517 518 519 520 521 522 523 524
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
function removeAllCallback(err) {
    if (err) {
E
ester.zhou 已提交
525
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
526 527 528 529 530
    } else {
        console.info("removeAll success");
    }
}

E
ester.zhou 已提交
531
notificationSubscribe.removeAll(removeAllCallback);
E
esterzhou 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
```

## 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      |
| ------ | ------------ | ---- | ---------- |
| bundle | [BundleOption](#bundleoption) | No  | Bundle information of the application.|

**Error codes**

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

E
esterzhou 已提交
556 557 558 559 560 561 562 563 564 565 566
| ID| Error Message                                |
| -------- | ---------------------------------------- |
| 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 已提交
567
notificationSubscribe.removeAll().then(() => {
E
esterzhou 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
	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 已提交
593 594
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

E
esterzhou 已提交
595 596 597 598 599 600 601 602 603 604 605 606
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 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 已提交
607
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
608 609 610 611 612
    } else {
        console.info("removeAll success");
    }
}

E
ester.zhou 已提交
613
let userId = 1;
E
esterzhou 已提交
614

E
ester.zhou 已提交
615
notificationSubscribe.removeAll(userId, removeAllCallback);
E
esterzhou 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
```

## 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 已提交
638 639
For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md).

E
esterzhou 已提交
640 641 642 643 644 645 646 647 648 649 650 651
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 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 已提交
652
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
653 654 655 656 657
    } else {
        console.info("removeAll success");
    }
}

E
ester.zhou 已提交
658
let userId = 1;
E
esterzhou 已提交
659

E
ester.zhou 已提交
660
notificationSubscribe.removeAll(userId, removeAllCallback);
E
esterzhou 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
```

## NotificationSubscriber

Provides callbacks for receiving or removing notifications and serves as the input parameter of [subscribe](#notificationsubscribe).

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

### onConsume

onConsume?: (data: [SubscribeCallbackData](#subscribecallbackdata)) => void

Callback for receiving notifications.

**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|
| ------------ | ------------------------ | ---- | -------------------------- |
| data | [SubscribeCallbackData](#subscribecallbackdata) | Yes| Information about the notification received.|

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
690
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
691 692 693 694 695 696 697 698 699 700 701
    } 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 已提交
702
let subscriber = {
E
esterzhou 已提交
703 704 705
    onConsume: onConsumeCallback
};

E
ester.zhou 已提交
706
notificationSubscribe.subscribe(subscriber, subscribeCallback);
E
esterzhou 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
```

### onCancel

onCancel?:(data: [SubscribeCallbackData](#subscribecallbackdata)) => void

Callback for canceling notifications.

**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|
| ------------ | ------------------------ | ---- | -------------------------- |
| data | [SubscribeCallbackData](#subscribecallbackdata) | Yes| Information about the notification to cancel.|

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
730
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
731 732 733 734 735 736 737 738 739 740 741
    } 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 已提交
742
let subscriber = {
E
esterzhou 已提交
743 744 745
    onCancel: onCancelCallback
};

E
ester.zhou 已提交
746
notificationSubscribe.subscribe(subscriber, subscribeCallback);
E
esterzhou 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
```

### onUpdate

onUpdate?:(data: [NotificationSortingMap](#notificationsortingmap)) => void

Callback for notification sorting updates.

**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|
| ------------ | ------------------------ | ---- | -------------------------- |
| data | [NotificationSortingMap](#notificationsortingmap) | Yes| Latest notification sorting list.|

**Example**

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

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

E
ester.zhou 已提交
780
let subscriber = {
E
esterzhou 已提交
781 782 783
    onUpdate: onUpdateCallback
};

E
ester.zhou 已提交
784
notificationSubscribe.subscribe(subscriber, subscribeCallback);
E
esterzhou 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
```

### onConnect

onConnect?:() => void

Callback for subscription.

**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 已提交
802
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
803 804 805 806 807 808 809 810 811
    } else {
        console.info("subscribeCallback");
    }
};

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

E
ester.zhou 已提交
812
let subscriber = {
E
esterzhou 已提交
813 814 815
    onConnect: onConnectCallback
};

E
ester.zhou 已提交
816
notificationSubscribe.subscribe(subscriber, subscribeCallback);
E
esterzhou 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
```

### onDisconnect

onDisconnect?:() => void

Callback for unsubscription.

**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 已提交
834
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
835 836 837 838 839 840
    } else {
        console.info("subscribeCallback");
    }
};
function unsubscribeCallback(err) {
    if (err.code) {
E
ester.zhou 已提交
841
        console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
842 843 844 845 846 847 848 849 850 851 852 853
    } else {
        console.info("unsubscribeCallback");
    }
};

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

E
ester.zhou 已提交
854
let subscriber = {
E
esterzhou 已提交
855 856 857 858 859
    onConnect: onConnectCallback,
    onDisconnect: onDisconnectCallback
};

// The onConnect callback is invoked when subscription to the notification is complete.
E
ester.zhou 已提交
860
notificationSubscribe.subscribe(subscriber, subscribeCallback);
E
esterzhou 已提交
861
// The onDisconnect callback is invoked when unsubscription to the notification is complete.
E
ester.zhou 已提交
862
notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
E
esterzhou 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
```

### 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 已提交
880
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
881 882 883 884 885 886 887 888 889
    } else {
        console.info("subscribeCallback");
    }
};

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

E
ester.zhou 已提交
890
let subscriber = {
E
esterzhou 已提交
891 892 893
    onDestroy: onDestroyCallback
};

E
ester.zhou 已提交
894
notificationSubscribe.subscribe(subscriber, subscribeCallback);
E
esterzhou 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
```

### onDoNotDisturbDateChange

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

Callback for DND time setting updates.

**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 已提交
918
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
919 920 921 922 923 924 925 926 927
    } else {
        console.info("subscribeCallback");
    }
};

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

E
ester.zhou 已提交
928
let subscriber = {
E
esterzhou 已提交
929 930 931
    onDoNotDisturbDateChange: onDoNotDisturbDateChangeCallback
};

E
ester.zhou 已提交
932
notificationSubscribe.subscribe(subscriber, subscribeCallback);
E
esterzhou 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
```


### onEnabledNotificationChanged

onEnabledNotificationChanged?:(callbackData: [EnabledNotificationCallbackData](#enablednotificationcallbackdata)) => void

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|
| ------------ | ------------------------ | ---- | -------------------------- |
| callback | AsyncCallback\<[EnabledNotificationCallbackData](#enablednotificationcallbackdata)\> | Yes| Callback used to return the result.|

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
E
ester.zhou 已提交
957
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
E
esterzhou 已提交
958 959 960 961 962 963 964 965 966 967 968
    } 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 已提交
969
let subscriber = {
E
esterzhou 已提交
970 971 972
    onEnabledNotificationChanged: onEnabledNotificationChangedCallback
};

E
ester.zhou 已提交
973
notificationSubscribe.subscribe(subscriber, subscribeCallback);
E
esterzhou 已提交
974 975
```

E
ester.zhou 已提交
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
### onBadgeChanged<sup>10+</sup>

 onBadgeChanged?:(data: [BadgeNumberCallbackData](#badgenumbercallbackdata)) => void

Listens for the change of the notification badge number.

**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                      |
| -------- | ------------------------------------------------------------ | ---- | -------------------------- |
| callback | AsyncCallback\<[BadgeNumberCallbackData](#badgenumbercallbackdata)\> | Yes  | Callback used to return the result.|

**Example**

```javascript
function subscribeCallback(err) {
    if (err) {
        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("subscribeCallback");
    }
};

function onBadgeChangedCallback(data) {
    console.info("bundle: ", data.bundle);
    console.info("uid: ", data.uid);
    console.info("badgeNumber: ", data.badgeNumber);
};

let subscriber = {
    onBadgeChanged: onBadgeChangedCallback
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);
```

E
esterzhou 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
## BundleOption

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

| Name  | Type  | Readable| Writable| Description  |
| ------ | ------ |---- | --- |  ------ |
| bundle | string | Yes | Yes | Bundle information of the application.|
| uid    | number | Yes | Yes | User ID.|

## NotificationKey

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

| Name | Type  | Readable| Writable| Description    |
| ----- | ------ | ---- | --- | -------- |
| id    | number | Yes | Yes | Notification ID.  |
| label | string | Yes | Yes | Notification label.|

## SubscribeCallbackData

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

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

| Name           | Type                                             | Readable                                           | Writable                                           | Description    |
| --------------- | ------------------------------------------------- | -------- | -------- | -------- |
| request         | [NotificationRequest](js-apis-notificationManager.md#notificationrequest) | Yes| No| Notification content.|
| sortingMap      | [NotificationSortingMap](#notificationsortingmap) | Yes| No| Notification sorting information.|
| reason          | number                                            | Yes                                          | No                                          | Reason for deletion.|
| sound           | string                                            | Yes                                          | No                                          | Sound used for notification.|
| vibrationValues | Array\<number\>                                   | Yes                                 | No                                 | Vibration used for notification.|


## EnabledNotificationCallbackData

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

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

| Name  | Type   | Readable | Writable | Description            |
| ------ | ------- | ---------------- | ---------------- | ---------------- |
| bundle | string  | Yes| No| Bundle name of the application.      |
| uid    | number  | Yes| No| UID of the application.       |
| enable | boolean | Yes| No| Notification enabled status of the application.|


## NotificationSorting

E
ester.zhou 已提交
1064
Provides sorting information of active notifications.
E
esterzhou 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

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

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

| Name    | Type                                 | Readable| Writable| Description        |
| -------- | ------------------------------------- | ---- | --- | ------------ |
| slot     | [NotificationSlot](js-apis-notificationManager.md#notificationslot) | Yes | No | Notification slot.|
| hashCode | string                                | Yes | No | Unique ID of the notification.|
| ranking  | number                                | Yes | No | Notification sequence number.|


## NotificationSortingMap

Provides sorting information of active notifications in all subscribed notifications.

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

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

| Name          | Type                                                        | Readable| Writable| Description            |
| -------------- | ------------------------------------------------------------ | ---- | --- | ---------------- |
| sortings       | {[key: string]: [NotificationSorting](#notificationsorting)} | Yes | No | Array of notification sorting information.|
| sortedHashCode | Array\<string\>                                              | Yes | No | Array of unique notification IDs.|


## NotificationSubscribeInfo

Provides the information about the publisher for notification subscription.

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

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

| Name       | Type           | Readable| Writable| Description                           |
| ----------- | --------------- | --- | ---- | ------------------------------- |
| bundleNames | Array\<string\> | Yes | Yes | Bundle names of the applications whose notifications are to be subscribed to.|
| userId      | number          | Yes | Yes | User whose notifications are to be subscribed to.   |


## NotificationUserInput

Provides the notification user input.

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

| Name    | Type  | Readable| Writable| Description                         |
| -------- | ------ | --- | ---- | ----------------------------- |
| inputKey | string | Yes | Yes | Key to identify the user input.|

## RemoveReason

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

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

| 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.        |
E
ester.zhou 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

## BadgeNumberCallbackData<sup>10+</sup>

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

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

| Name       | Type  | Readable| Writable| Description        |
| ----------- | ------ | ---- | ---- | ------------ |
| bundle      | string | Yes  | No  | Bundle name of the application.|
| uid         | number | Yes  | No  | UID of the application. |
| badgeNumber | number | Yes  | No  | Notification badge number.  |