未验证 提交 e2deb1b9 编写于 作者: O openharmony_ci 提交者: Gitee

!4052 3345 处理完成:notification文件夹下文件添加samples

Merge pull request !4052 from ester.zhou/TR-3345
...@@ -13,7 +13,7 @@ Each application can subscribe to common events as required. After your applicat ...@@ -13,7 +13,7 @@ Each application can subscribe to common events as required. After your applicat
## Common Event Subscription Development ## Common Event Subscription Development
### When to Use ### When to Use
You can create a subscriber object to subscribe to a common event to obtain the parameters passed in the event. Certain system common events require specific permissions to subscribe to. For details, see [Required Permissions](../reference/apis/js-apis-commonEvent.md). You can create a subscriber object to subscribe to a common event to obtain the parameters passed in the event. Certain system common events require specific permissions to subscribe to. For details, see [Required Permissions](../reference/apis/js-apis-commonEvent.md#required-permissions).
### Available APIs ### Available APIs
| API | Description| | API | Description|
...@@ -170,10 +170,8 @@ if (this.subscriber != null) { ...@@ -170,10 +170,8 @@ if (this.subscriber != null) {
} }
``` ```
## Development Example ## Samples
The following sample is provided to help you better understand how to use the common event functionality: The following sample is provided to help you better understand how to use the common event functionality:
- [CommonEvent](https://gitee.com/openharmony/app_samples/tree/master/ability/CommonEvent) - [`CommonEvent`: eTS Common Event (API 8)](https://gitee.com/openharmony/app_samples/tree/master/Notification/CommonEvent)
This sample shows how to use **CommonEvent** APIs in Extended TypeScript (eTS) to create subscribers and subscribe to, publish, and unsubscribe from common events.
...@@ -131,13 +131,25 @@ var subscriber = { ...@@ -131,13 +131,25 @@ var subscriber = {
### Publishing Notifications ### Publishing Notifications
Before publishing a notification, make sure the notification feature is enabled for your application. This feature is disabled by default and can be enabled in the notification settings. ##### Enabling Notification
Before publishing a notification, check whether the notification feature is enabled for your application. By default, the feature is disabled. The application calls **Notification.requestEnableNotification** to prompt the user to enable the feature.
```js
Notification.requestEnableNotification() .then((data) => {
console.info('===>requestEnableNotification success');
}).catch((err) => {
console.error('===>requestEnableNotification failed because ' + JSON.stringify(err));
});
```
##### Publishing Notifications ##### Publishing Notifications
To publish a notification, create a **NotificationRequest** object and set attributes such as the notification type, title, and content. In the following examples, a normal text notification and a notification containing a **WantAgent** are being published. To publish a notification, create a **NotificationRequest** object and set attributes such as the notification type, title, and content. In the following examples, a normal text notification and a notification containing a **WantAgent** are being published.
Normal Text Notification Normal text notification:
```js ```js
// Create a NotificationRequest object. // Create a NotificationRequest object.
...@@ -163,7 +175,7 @@ Notification.publish(notificationRequest) .then((data) => { ...@@ -163,7 +175,7 @@ Notification.publish(notificationRequest) .then((data) => {
Notification Containing WantAgent. Notification containing **WantAgent**:
For details about how to use **WantAgent**, see [WantAgent Development](https://gitee.com/openharmony/docs/blob/master/en/application-dev/ability/wantagent.md). For details about how to use **WantAgent**, see [WantAgent Development](https://gitee.com/openharmony/docs/blob/master/en/application-dev/ability/wantagent.md).
...@@ -234,7 +246,7 @@ Notification.publish(notificationRequest) .then((data) => { ...@@ -234,7 +246,7 @@ Notification.publish(notificationRequest) .then((data) => {
- Cancel the notification. - Cancel the notification.
An application can cancel a single notification or all notifications. An application can cancel only the notifications published by itself. An application can cancel a single notification or all notifications. An application can cancel only the notifications published by itself.
```js ```js
// cancel callback // cancel callback
...@@ -246,3 +258,8 @@ Notification.cancel(1, "label", cancelCallback) ...@@ -246,3 +258,8 @@ Notification.cancel(1, "label", cancelCallback)
``` ```
## Samples
The following sample is provided to help you better understand how to develop notification functions:
[`Notification`: Notification (eTS, API 8)](https://gitee.com/openharmony/app_samples/tree/master/common/Notification)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册