js-apis-notificationSubscribe.md 29.0 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.notificationSubscribe (NotificationSubscribe模块)
2 3 4 5 6 7 8 9 10

本模块提供通知订阅、取消订阅、通知移除等,一般情况下,只有系统应用具有这些操作权限。

> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

## 导入模块

X
xuzhihao 已提交
11
```ts
12
import notificationSubscribe from '@ohos.notificationSubscribe';
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
```

## NotificationSubscribe.subscribe

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

订阅通知并指定订阅信息(callback形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
29
| 参数名       | 类型                      | 必填 | 说明             |
30
| ---------- | ------------------------- | ---- | ---------------- |
Y
yuyaozhi 已提交
31 32
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber)    | 是   | 通知订阅对象。     |
| info       | [NotificationSubscribeInfo](js-apis-notification.md#notificationsubscribeinfo) | 是   | 通知订阅信息。 |
33 34 35 36
| callback   | AsyncCallback\<void\>     | 是   | 订阅动作回调函数。 |

**错误码:**

F
fangJinliang1 已提交
37 38
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

39 40 41 42 43
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
44
| 1600012  | No memory space.                    |
45 46 47

**示例:**

X
xuzhihao 已提交
48 49 50
```ts
import Base from '@ohos.base';

51
//subscribe回调
X
xuzhihao 已提交
52 53 54 55 56 57
let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribe success");
  }
58
}
X
xuzhihao 已提交
59 60
let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
  console.info("Consume callback: " + JSON.stringify(data));
61
}
X
xuzhihao 已提交
62 63
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
F
fangJinliang1 已提交
64
};
X
xuzhihao 已提交
65 66
let info: notificationSubscribe.NotificationSubscribeInfo = {
  bundleNames: ["bundleName1","bundleName2"]
F
fangJinliang1 已提交
67
};
68
notificationSubscribe.subscribe(subscriber, info, subscribeCallback);
69 70 71 72 73 74
```

## NotificationSubscribe.subscribe

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

F
fangJinliang1 已提交
75
订阅当前用户下所有应用的通知(callback形式)。
76 77 78 79 80 81 82 83 84

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
85
| 参数名       | 类型                   | 必填 | 说明             |
86
| ---------- | ---------------------- | ---- | ---------------- |
Y
yuyaozhi 已提交
87
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | 是   | 通知订阅对象。     |
88 89 90 91
| callback   | AsyncCallback\<void\>  | 是   | 订阅动作回调函数。 |

**错误码:**

F
fangJinliang1 已提交
92 93
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

94 95 96 97 98
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
99
| 1600012  | No memory space.                    |
100 101 102

**示例:**

X
xuzhihao 已提交
103 104 105 106 107 108 109 110 111
```ts
import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribe success");
  }
112
}
X
xuzhihao 已提交
113 114
let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
  console.info("Consume callback: " + JSON.stringify(data));
115
}
X
xuzhihao 已提交
116 117
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
F
fangJinliang1 已提交
118
};
119
notificationSubscribe.subscribe(subscriber, subscribeCallback);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
```



## NotificationSubscribe.subscribe

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

订阅通知并指定订阅信息(Promise形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
138
| 参数名       | 类型                      | 必填 | 说明         |
139
| ---------- | ------------------------- | ---- | ------------ |
Y
yuyaozhi 已提交
140
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber)    | 是   | 通知订阅对象。 |
W
wangkailong 已提交
141
| info       | [NotificationSubscribeInfo](js-apis-notification.md#notificationsubscribeinfo) | 否   | 通知订阅信息,默认为空。   |
142 143 144

**错误码:**

F
fangJinliang1 已提交
145 146
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

147 148 149 150 151
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
152
| 1600012  | No memory space.                    |
153 154 155

**示例:**

X
xuzhihao 已提交
156 157 158 159 160
```ts
import Base from '@ohos.base';

let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
  console.info("Consume callback: " + JSON.stringify(data));
161
}
X
xuzhihao 已提交
162 163
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
164
};
165
notificationSubscribe.subscribe(subscriber).then(() => {
X
xuzhihao 已提交
166 167 168
  console.info("subscribe success");
}).catch((err: Base.BusinessError) => {
  console.error("subscribe fail: " + JSON.stringify(err));
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
});
```



## NotificationSubscribe.unsubscribe

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

取消订阅(callbcak形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
188
| 参数名       | 类型                   | 必填 | 说明                 |
189
| ---------- | ---------------------- | ---- | -------------------- |
Y
yuyaozhi 已提交
190
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | 是   | 通知订阅对象。         |
191 192 193 194
| callback   | AsyncCallback\<void\>  | 是   | 取消订阅动作回调函数。 |

**错误码:**

F
fangJinliang1 已提交
195 196
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

197 198 199 200 201 202 203 204
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**示例:**

X
xuzhihao 已提交
205 206 207 208 209 210 211 212 213
```ts
import Base from '@ohos.base';

let unsubscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("unsubscribe success");
  }
214
}
X
xuzhihao 已提交
215 216
let onDisconnectCallback = () => {
  console.info("subscribe disconnect");
217
}
X
xuzhihao 已提交
218 219
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onDisconnect: onDisconnectCallback
F
fangJinliang1 已提交
220
};
221
notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
```

## NotificationSubscribe.unsubscribe

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

取消订阅(Promise形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
238
| 参数名       | 类型                   | 必填 | 说明         |
239
| ---------- | ---------------------- | ---- | ------------ |
Y
yuyaozhi 已提交
240
| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | 是   | 通知订阅对象。 |
241 242 243

**错误码:**

F
fangJinliang1 已提交
244 245
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

246 247 248 249 250 251 252 253
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**示例:**

X
xuzhihao 已提交
254 255 256 257 258
```ts
import Base from '@ohos.base';

let onDisconnectCallback = () => {
  console.info("subscribe disconnect");
259
}
X
xuzhihao 已提交
260 261
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onDisconnect: onDisconnectCallback
262
};
263
notificationSubscribe.unsubscribe(subscriber).then(() => {
X
xuzhihao 已提交
264 265 266
  console.info("unsubscribe success");
}).catch((err: Base.BusinessError) => {
  console.error("unsubscribe fail: " + JSON.stringify(err));
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
});
```

## NotificationSubscribe.remove

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

删除指定通知(Callback形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
284
| 参数名            | 类型                                | 必填 | 说明                 |
285
| --------------- |   ----------------------------------| ---- | -------------------- |
Y
yuyaozhi 已提交
286
| bundle          | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)       | 是   | 指定应用的包信息。           |
287
| notificationKey | [NotificationKey](#notificationkey) | 是   | 通知键值。             |
288 289 290 291 292
| reason          | [RemoveReason](#removereason)      | 是   | 通知删除原因。         |
| callback        | AsyncCallback\<void\>               | 是   | 删除指定通知回调函数。 |

**错误码:**

F
fangJinliang1 已提交
293 294
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

295 296 297 298 299 300 301 302 303 304
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 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. |

**示例:**

X
xuzhihao 已提交
305 306 307 308 309 310 311 312 313 314
```ts
import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

let removeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("remove success");
  }
315
}
X
xuzhihao 已提交
316 317
let bundle: NotificationManager.BundleOption = {
  bundle: "bundleName1",
F
fangJinliang1 已提交
318
};
X
xuzhihao 已提交
319 320 321
let notificationKey: notificationSubscribe.NotificationKey = {
  id: 0,
  label: "label",
F
fangJinliang1 已提交
322
};
X
xuzhihao 已提交
323
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
324
notificationSubscribe.remove(bundle, notificationKey, reason, removeCallback);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
```



## NotificationSubscribe.remove

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

删除指定通知(Promise形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
343
| 参数名            | 类型            | 必填 | 说明       |
344
| --------------- | --------------- | ---- | ---------- |
Y
yuyaozhi 已提交
345
| bundle          | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)    | 是   | 指定应用的包信息。 |
346
| notificationKey | [NotificationKey](#notificationkey) | 是   | 通知键值。   |
347 348 349 350
| reason          | [RemoveReason](#removereason) | 是   | 通知删除原因。         |

**错误码:**

F
fangJinliang1 已提交
351 352
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

353 354 355 356 357 358 359 360 361 362
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 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. |

**示例:**

X
xuzhihao 已提交
363 364 365 366 367 368
```ts
import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

let bundle: NotificationManager.BundleOption = {
  bundle: "bundleName1",
F
fangJinliang1 已提交
369
};
X
xuzhihao 已提交
370 371 372
let notificationKey: notificationSubscribe.NotificationKey = {
  id: 0,
  label: "label",
F
fangJinliang1 已提交
373
};
X
xuzhihao 已提交
374
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
375
notificationSubscribe.remove(bundle, notificationKey, reason).then(() => {
X
xuzhihao 已提交
376 377 378
  console.info("remove success");
}).catch((err: Base.BusinessError) => {
  console.error("remove fail: " + JSON.stringify(err));
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
});
```

## NotificationSubscribe.remove

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

删除指定通知(Callback形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
396
| 参数名     | 类型                  | 必填 | 说明                 |
397
| -------- | --------------------- | ---- | -------------------- |
398
| hashCode | string                | 是   | 通知唯一ID。可以通过[onConsume](js-apis-inner-notification-notificationSubscriber.md#onConsume)回调的入参[SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)获取其内部[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象中的hashCode。 |
399 400 401 402 403
| reason   | [RemoveReason](#removereason) | 是   | 通知删除原因。         |
| callback | AsyncCallback\<void\> | 是   | 删除指定通知回调函数。 |

**错误码:**

F
fangJinliang1 已提交
404 405
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

406 407 408 409 410 411 412 413 414
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600007  | The notification is not exist.      |

**示例:**

X
xuzhihao 已提交
415 416
```ts
import Base from '@ohos.base';
417

X
xuzhihao 已提交
418 419 420 421 422 423 424 425
let hashCode: string = 'hashCode';

let removeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("remove success");
  }
426
}
X
xuzhihao 已提交
427
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
428
notificationSubscribe.remove(hashCode, reason, removeCallback);
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
```

## NotificationSubscribe.remove

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

删除指定通知(Promise形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
445
| 参数名     | 类型       | 必填 | 说明       |
446 447 448 449 450 451
| -------- | ---------- | ---- | ---------- |
| hashCode | string | 是   | 通知唯一ID。 |
| reason   | [RemoveReason](#removereason) | 是   | 通知删除原因。         |

**错误码:**

F
fangJinliang1 已提交
452 453
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

454 455 456 457 458 459 460 461 462
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600007  | The notification is not exist.      |

**示例:**

X
xuzhihao 已提交
463 464 465 466 467
```ts
import Base from '@ohos.base';

let hashCode: string = 'hashCode';
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
468
notificationSubscribe.remove(hashCode, reason).then(() => {
F
fangJinliang1 已提交
469
	console.info("remove success");
X
xuzhihao 已提交
470 471
}).catch((err: Base.BusinessError) => {
  console.error("remove fail: " + JSON.stringify(err));
472 473
});
```
X
XKK 已提交
474
## NotificationSubscribe.remove<sup>10+<sup>
X
XKK 已提交
475

X
XKK 已提交
476
remove(hashCodes: Array\<String\>, reason: RemoveReason, callback: AsyncCallback\<void\>): void
X
XKK 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489

批量删除指定通知(Callback形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名       | 类型                            | 必填 | 说明                                                                                                                                                                                                                                                                                  |
|-----------|-------------------------------| ---- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
X
XKK 已提交
490
| hashCodes | Array\<String\>               | 是   | 通知唯一ID数组集合。可以通过[onConsume](js-apis-inner-notification-notificationSubscriber.md#onConsume)回调的入参[SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)获取其内部[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象中的hashCode。 |
X
XKK 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
| reason    | [RemoveReason](#removereason) | 是   | 通知删除原因。                                                                                                                                                                                                                                                                             |
| callback  | AsyncCallback\<void\>         | 是   | 删除指定通知回调函数。                                                                                                                                                                                                                                                                         |

**错误码:**

错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**示例:**

X
xuzhihao 已提交
506 507
```ts
import Base from '@ohos.base';
X
XKK 已提交
508

X
xuzhihao 已提交
509 510 511 512 513 514 515 516
let hashCodes: string[] = ['hashCode1', 'hashCode2'];

let removeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("remove success");
  }
X
XKK 已提交
517
}
X
xuzhihao 已提交
518
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
X
XKK 已提交
519 520 521
notificationSubscribe.remove(hashCodes, reason, removeCallback);
```

X
XKK 已提交
522
## NotificationSubscribe.remove<sup>10+<sup>
X
XKK 已提交
523

X
XKK 已提交
524
remove(hashCodes: Array\<String\>, reason: RemoveReason): Promise\<void\>
X
XKK 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537

批量删除指定通知(Promise形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

| 参数名       | 类型                            | 必填 | 说明          |
|-----------|-------------------------------| ---- |-------------|
X
XKK 已提交
538
| hashCodes | Array\<String\>               | 是   | 通知唯一ID数组集合。 |
X
XKK 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552
| reason    | [RemoveReason](#removereason) | 是   | 通知删除原因。     |

**错误码:**

错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**示例:**

X
xuzhihao 已提交
553 554 555 556 557
```ts
import Base from '@ohos.base';

let hashCodes: string[] = ['hashCode1','hashCode2'];
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
X
XKK 已提交
558
notificationSubscribe.remove(hashCodes, reason).then(() => {
X
xuzhihao 已提交
559 560 561
  console.info("remove success");
}).catch((err: Base.BusinessError) => {
  console.error("remove fail: " + JSON.stringify(err));
X
XKK 已提交
562 563
});
```
564 565 566 567 568

## NotificationSubscribe.removeAll

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

F
fangJinliang1 已提交
569
删除指定应用的所有通知(Callback形式)。
570 571 572 573 574 575 576 577 578

**系统能力**:SystemCapability.Notification.Notification

**系统API**:此接口为系统接口,三方应用不支持调用。

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**参数:**

F
fangJinliang1 已提交
579
| 参数名     | 类型                  | 必填 | 说明                         |
580
| -------- | --------------------- | ---- | ---------------------------- |
Z
zengyawen 已提交
581
| bundle   | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)        | 是   | 指定应用的包信息。                   |
F
fangJinliang1 已提交
582
| callback | AsyncCallback\<void\> | 是   | 删除指定应用的所有通知回调函数。 |
583 584 585

**错误码:**

F
fangJinliang1 已提交
586 587
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

588 589 590 591 592 593 594 595 596
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 1600001  | Internal error.                          |
| 1600002  | Marshalling or unmarshalling error.      |
| 1600003  | Failed to connect service.               |
| 17700001 | The specified bundle name was not found. |

**示例:**

X
xuzhihao 已提交
597 598 599 600 601 602 603 604 605
```ts
import Base from '@ohos.base';

let removeAllCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("removeAll success");
  }
606
}
X
xuzhihao 已提交
607 608
let bundle: notificationSubscribe.BundleOption = {
  bundle: "bundleName1",
F
fangJinliang1 已提交
609
};
610
notificationSubscribe.removeAll(bundle, removeAllCallback);
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
```

## NotificationSubscribe.removeAll

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

删除所有通知(Callback形式)。

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
627
| 参数名     | 类型                  | 必填 | 说明                 |
628 629 630 631 632
| -------- | --------------------- | ---- | -------------------- |
| callback | AsyncCallback\<void\> | 是   | 删除所有通知回调函数。 |

**错误码:**

F
fangJinliang1 已提交
633 634
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

635 636 637 638 639 640 641 642
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |

**示例:**

X
xuzhihao 已提交
643 644 645 646
```ts
import Base from '@ohos.base';

let removeAllCallback = (err: Base.BusinessError) => {
647
    if (err) {
648
        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
649 650 651 652 653
    } else {
        console.info("removeAll success");
    }
}

654
notificationSubscribe.removeAll(removeAllCallback);
655 656 657 658 659 660
```

## NotificationSubscribe.removeAll

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

F
fangJinliang1 已提交
661
删除指定应用的所有通知(Promise形式)。
662 663 664 665 666 667 668 669 670

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
671
| 参数名   | 类型         | 必填 | 说明       |
672
| ------ | ------------ | ---- | ---------- |
W
wangkailong 已提交
673
| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | 否   | 指定应用的包信息。默认为空,表示删除所有通知。 |
674 675 676

**错误码:**

F
fangJinliang1 已提交
677 678
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

679 680 681 682 683 684 685 686 687
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 1600001  | Internal error.                          |
| 1600002  | Marshalling or unmarshalling error.      |
| 1600003  | Failed to connect service.               |
| 17700001 | The specified bundle name was not found. |

**示例:**

X
xuzhihao 已提交
688 689 690
```ts
import Base from '@ohos.base';

F
fangJinliang1 已提交
691
// 不指定应用时,删除所有通知
692
notificationSubscribe.removeAll().then(() => {
F
fangJinliang1 已提交
693
	console.info("removeAll success");
X
xuzhihao 已提交
694 695
}).catch((err: Base.BusinessError) => {
  console.error("removeAll fail: " + JSON.stringify(err));
696 697 698 699 700 701 702
});
```

## NotificationSubscribe.removeAll

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

F
fangJinliang1 已提交
703
删除指定用户下的所有通知(callback形式)。
704 705 706 707 708 709 710 711 712

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
713
| 参数名   | 类型         | 必填 | 说明       |
714
| ------ | ------------ | ---- | ---------- |
F
fangJinliang1 已提交
715 716
| userId | number | 是   | 用户ID。 |
| callback | AsyncCallback\<void\> | 是   | 删除指定用户所有通知回调函数。 |
717 718 719

**错误码:**

F
fangJinliang1 已提交
720 721
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

722 723 724 725 726 727 728 729 730
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600008  | The user is not exist.              |

**示例:**

X
xuzhihao 已提交
731 732 733 734 735 736 737 738 739
```ts
import Base from '@ohos.base';

let removeAllCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("removeAll success");
  }
740 741
}

X
xuzhihao 已提交
742
let userId: number = 1;
743

744
notificationSubscribe.removeAll(userId, removeAllCallback);
745 746
```

747
## NotificationSubscribe.removeAll
748 749 750

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

F
fangJinliang1 已提交
751
删除指定用户下的所有通知(Promise形式)。
752 753 754 755 756 757 758 759 760

**系统能力**:SystemCapability.Notification.Notification

**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

F
fangJinliang1 已提交
761
| 参数名   | 类型         | 必填 | 说明       |
762
| ------ | ------------ | ---- | ---------- |
F
fangJinliang1 已提交
763
| userId | number | 是   | 用户ID。 |
764 765 766

**错误码:**

F
fangJinliang1 已提交
767 768
错误码详细介绍请参考[errcode-notification](../errorcodes/errorcode-notification.md)

769 770 771 772 773 774 775 776 777
| 错误码ID | 错误信息                            |
| -------- | ----------------------------------- |
| 1600001  | Internal error.                     |
| 1600002  | Marshalling or unmarshalling error. |
| 1600003  | Failed to connect service.          |
| 1600008  | The user is not exist.              |

**示例:**

X
xuzhihao 已提交
778 779 780 781 782 783 784 785 786
```ts
import Base from '@ohos.base';

let removeAllCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("removeAll success");
  }
787 788
}

X
xuzhihao 已提交
789
let userId: number = 1;
790

791
notificationSubscribe.removeAll(userId, removeAllCallback);
792 793
```

794 795 796 797
## NotificationKey

**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification

F
fangJinliang1 已提交
798 799
**系统API**: 此接口为系统接口,三方应用不支持调用。

800 801 802
| 名称  | 类型   | 必填 | 说明     |
| ----- | ------ | --- | -------- |
| id    | number | 是  | 通知ID。   |
W
wangkailong 已提交
803
| label | string | 否  | 通知标签,默认为空。 |
804

805 806 807 808 809 810
## RemoveReason

**系统能力**:SystemCapability.Notification.Notification

**系统API**: 此接口为系统接口,三方应用不支持调用。

F
fangJinliang1 已提交
811
| 名称                 | 值  | 说明                  |
812 813
| -------------------- | --- | -------------------- |
| CLICK_REASON_REMOVE  | 1   | 点击通知后删除通知。    |
F
fangJinliang1 已提交
814
| CANCEL_REASON_REMOVE | 2   | 用户删除通知。         |