js-apis-notificationSubscribe.md 32.8 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
```



## 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.|
E
esterzhou 已提交
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).

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
```

## 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 已提交
86
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes  | Notification subscriber.    |
E
esterzhou 已提交
87 88 89 90
| 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
```



## 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 已提交
136 137
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber)    | Yes  | Notification subscriber.|
| info       | [NotificationSubscribeInfo](js-apis-notification.md#notificationsubscribeinfo) | No  | Notification subscription information.  |
E
esterzhou 已提交
138 139 140

**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
	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 已提交
181
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes  | Notification subscriber.        |
E
esterzhou 已提交
182 183 184 185
| 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
    } else {
        console.info("unsubscribe success");
    }
}
E
ester.zhou 已提交
204 205
function onDisconnectCallback() {
	console.info("subscribe disconnect");
E
esterzhou 已提交
206
}
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
```

## 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 已提交
229
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes  | Notification subscriber.|
E
esterzhou 已提交
230 231 232

**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
| ID| Error Message                           |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**Example**

```js
E
ester.zhou 已提交
244 245
function onDisconnectCallback() {
	console.info("subscribe disconnect");
E
esterzhou 已提交
246
}
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
	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 已提交
271 272
| 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.            |
E
esterzhou 已提交
273 274 275 276 277
| 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
```



## 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 已提交
327 328
| 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.  |
E
esterzhou 已提交
329 330 331 332
| 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
	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 已提交
375
| 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. |
E
esterzhou 已提交
376 377 378 379 380
| 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
	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 已提交
462
| bundle   | [BundleOption]((js-apis-inner-notification-notificationCommonDef.md#bundleoption))          | Yes  | Bundle information of the application.                  |
E
esterzhou 已提交
463 464 465 466
| 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
```

## 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 已提交
550
| bundle | [BundleOption]((js-apis-inner-notification-notificationCommonDef.md#bundleoption)) | No  | Bundle information of the application.|
E
esterzhou 已提交
551 552 553

**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
```

## 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

E
ester.zhou 已提交
671
onConsume?: (data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void
E
esterzhou 已提交
672 673 674 675 676 677 678 679 680 681 682

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|
| ------------ | ------------------------ | ---- | -------------------------- |
E
ester.zhou 已提交
683
| data | [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) | Yes| Information about the notification received.|
E
esterzhou 已提交
684 685 686 687 688 689

**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
```

### onCancel

E
ester.zhou 已提交
711
onCancel?:(data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void
E
esterzhou 已提交
712 713 714 715 716 717 718 719 720 721 722

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|
| ------------ | ------------------------ | ---- | -------------------------- |
E
ester.zhou 已提交
723
| data | [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) | Yes| Information about the notification to cancel.|
E
esterzhou 已提交
724 725 726 727 728 729

**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
```

### onUpdate

E
ester.zhou 已提交
751
onUpdate?:(data: [NotificationSortingMap](js-apis-notification.md#notificationsortingmap)) => void
E
esterzhou 已提交
752 753 754 755 756 757 758 759 760 761 762

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|
| ------------ | ------------------------ | ---- | -------------------------- |
E
ester.zhou 已提交
763
| data | [NotificationSortingMap](js-apis-notification.md#notificationsortingmap)) | Yes| Latest notification sorting list.|
E
esterzhou 已提交
764 765 766 767 768 769

**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
```


### onEnabledNotificationChanged

E
ester.zhou 已提交
938
onEnabledNotificationChanged?:(callbackData: [EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata)) => void
E
esterzhou 已提交
939 940 941 942 943 944 945 946 947 948 949

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 已提交
950
| callback | AsyncCallback\<[EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata)\> | Yes| Callback used to return the result.|
E
esterzhou 已提交
951 952 953 954 955 956

**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

## 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 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

## 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.  |