• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 发布基础类型通知
2
3
4基础类型通知主要应用于发送短信息、提示信息、广告推送等,支持普通文本类型、长文本类型、多行文本类型和图片类型。
5
6
7  **表1** 基础类型通知中的内容分类
8
9| 类型 | 描述 |
10| -------- | -------- |
11| NOTIFICATION_CONTENT_BASIC_TEXT | 普通文本类型。 |
12| NOTIFICATION_CONTENT_LONG_TEXT | 长文本类型。 |
13| NOTIFICATION_CONTENT_MULTILINE | 多行文本类型。 |
14| NOTIFICATION_CONTENT_PICTURE | 图片类型。 |
15
16
17目前系统仅通知栏订阅了通知,将通知显示在通知栏里。基础类型通知呈现效果示意图如下所示。
18
19
20  **图1** 基础类型通知呈现效果示意图  ![zh-cn_image_0000001466462305](figures/zh-cn_image_0000001466462305.png)
21
22
23## 接口说明
24
25通知发布接口如下表所示,不同发布类型通知由[NotificationRequest](../reference/apis/js-apis-notification.md#notificationrequest)的字段携带不同的信息。
26
27| **接口名** | **描述** |
28| -------- | -------- |
29| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | 发布通知。                 |
30| cancel(id: number, label: string, callback: AsyncCallback<void>): void | 取消指定的通知。           |
31| cancelAll(callback: AsyncCallback<void>): void; | 取消所有该应用发布的通知。 |
32
33
34## 开发步骤
35
361. [使能通知开关](notification-enable.md),获得用户授权后,才能使用通知功能。
37
382. 导入模块。
39
40   ```ts
41   import NotificationManager from '@ohos.notificationManager';
42   ```
43
443. 构造NotificationRequest对象,并发布通知。
45   - 普通文本类型通知由标题、文本内容和附加信息三个字段组成,其中标题和文本内容是必填字段,大小均需要小于200字节。
46
47      ```ts
48      let notificationRequest = {
49        id: 1,
50        content: {
51      	contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
52      	normal: {
53      	  title: 'test_title',
54      	  text: 'test_text',
55      	  additionalText: 'test_additionalText',
56      	}
57        }
58      }
59
60      NotificationManager.publish(notificationRequest, (err) => {
61          if (err) {
62              console.error(`[ANS] failed to publish, error[${err}]`);
63              return;
64          }
65          console.info(`[ANS] publish success`);
66      });
67      ```
68
69      运行效果如下图所示。
70
71     ![zh-cn_image_0000001466782033](figures/zh-cn_image_0000001466782033.png)
72   - 长文本类型通知继承了普通文本类型的字段,同时新增了长文本内容、内容概要和通知展开时的标题,其中长文本内容不超过1024字节,其他字段小于200字节。通知默认显示与普通文本相同,展开后,标题显示为展开后标题内容,内容为长文本内容。
73
74      ```ts
75      let notificationRequest = {
76        id: 1,
77        content: {
78      	contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 长文本类型通知
79      	longText: {
80      	  title: 'test_title',
81      	  text: 'test_text',
82      	  additionalText: 'test_additionalText',
83      	  longText: 'test_longText',
84      	  briefText: 'test_briefText',
85      	  expandedTitle: 'test_expandedTitle',
86      	}
87        }
88      }
89
90      // 发布通知
91      NotificationManager.publish(notificationRequest, (err) => {
92          if (err) {
93              console.error(`[ANS] failed to publish, error[${err}]`);
94              return;
95          }
96          console.info(`[ANS] publish success`);
97      });
98      ```
99
100      运行效果如下图所示。
101     ![zh-cn_image_0000001416745530](figures/zh-cn_image_0000001416745530.png)
102   - 多行文本类型通知继承了普通文本类型的字段,同时新增了多行文本内容、内容概要和通知展开时的标题,其字段均小于200字节。通知默认显示与普通文本相同,展开后,标题显示为展开后标题内容,多行文本内容多行显示。
103
104      ```ts
105      let notificationRequest = {
106        id: 1,
107        content: {
108      	contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // 多行文本类型通知
109      	multiLine: {
110      	  title: 'test_title',
111      	  text: 'test_text',
112      	  briefText: 'test_briefText',
113      	  longTitle: 'test_longTitle',
114      	  lines: ['line_01', 'line_02', 'line_03', 'line_04'],
115      	}
116        }
117      }
118
119      // 发布通知
120      NotificationManager.publish(notificationRequest, (err) => {
121        if (err) {
122      	console.error(`[ANS] failed to publish, error[${err}]`);
123      	return;
124        }
125        console.info(`[ANS] publish success`);
126      });
127      ```
128
129      运行效果如下图所示。
130     ![zh-cn_image_0000001417062446](figures/zh-cn_image_0000001417062446.png)
131   - 图片类型通知继承了普通文本类型的字段,同时新增了图片内容、内容概要和通知展开时的标题,图片内容为PixelMap型对象,其大小不能超过2M。
132
133      ```ts
134      let notificationPicture: PixelMap = undefined; // 需要获取图片PixelMap信息
135      let notificationRequest = {
136          id: 1,
137          content: {
138      	contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
139      	picture: {
140      	  title: 'test_title',
141      	  text: 'test_text',
142      	  additionalText: 'test_additionalText',
143      	  briefText: 'test_briefText',
144      	  expandedTitle: 'test_expandedTitle',
145      	  picture: notificationPicture
146      	}
147          }
148      }
149
150      // 发布通知
151      NotificationManager.publish(notificationRequest, (err) => {
152          if (err) {
153      	console.error(`[ANS] failed to publish, error[${err}]`);
154      	return;
155          }
156          console.info(`[ANS] publish success `);
157      });
158      ```
159
160      运行效果如下图所示。
161     ![zh-cn_image_0000001466582045](figures/zh-cn_image_0000001466582045.png)
162