• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Publishing a Basic Notification
2
3
4You can publish basic notifications to send SMS messages, prompt messages, and advertisements. Available content types of basic notifications include normal text, long text, multi-line text, and picture-attached.
5
6
7  **Table 1** Basic notification content types
8
9| Type| Description|
10| -------- | -------- |
11| NOTIFICATION_CONTENT_BASIC_TEXT | Normal text notification.|
12| NOTIFICATION_CONTENT_LONG_TEXT | Long text notification.|
13| NOTIFICATION_CONTENT_MULTILINE | Multi-line text notification.|
14| NOTIFICATION_CONTENT_PICTURE | Picture-attached notification.|
15
16
17Notifications are displayed in the notification panel, which is the only system subscriber to notifications. Below you can see two examples of the basic notification.
18
19**Figure 1** Examples of the basic notification
20![en-us_image_0000001466462305](figures/en-us_image_0000001466462305.png)
21
22
23## Available APIs
24
25The following table describes the APIs for notification publishing. You specify the notification type by setting the [NotificationRequest](../reference/apis/js-apis-notificationManager.md#notificationrequest) parameter in the APIs.
26
27| Name| Description|
28| -------- | -------- |
29| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | Publishes a notification.                |
30| cancel(id: number, label: string, callback: AsyncCallback<void>): void | Cancels a notification.          |
31| cancelAll(callback: AsyncCallback<void>): void; | Cancels all notifications published by the application.|
32
33
34## How to Develop
35
361. [Enable notification](notification-enable.md). An application can use the notification feature only after being authorized by the user.
37
382. Import the module.
39
40   ```ts
41   import notificationManager from '@ohos.notificationManager';
42   ```
43
443. Create a **NotificationRequest** object and publish a progress notification.
45   - A normal text notification consists of the **title**, **text**, and **additionalText** parameters, of which **title** and **text** are mandatory. The value of these parameters contains less than 200 bytes.
46
47      ```ts
48      let notificationRequest = {
49        id: 1,
50        content: {
51      	contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // Basic notification
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      Below is an example of the normal text notification.
70     ![en-us_image_0000001466782033](figures/en-us_image_0000001466782033.png)
71   - In addition to the parameters in the normal text notification, the long text notification provides the **longText**, **briefText**, and **expandedTitle** parameters. The value of **longText** contains a maximum of 1024 bytes, while that of any other parameters contains less than 200 bytes. By default, a long-text notification looks in the same way as a normal text notification. When expanded, the notification displays the title and content specified in **expandedTitle** and **longText**, respectively.
72
73      ```ts
74      let notificationRequest = {
75        id: 1,
76        content: {
77      	contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // Long-text notification
78      	longText: {
79      	  title: 'test_title',
80      	  text: 'test_text',
81      	  additionalText: 'test_additionalText',
82      	  longText: 'test_longText',
83      	  briefText: 'test_briefText',
84      	  expandedTitle: 'test_expandedTitle',
85      	}
86        }
87      }
88
89      // Publish the notification.
90      notificationManager.publish(notificationRequest, (err) => {
91          if (err) {
92              console.error(`[ANS] failed to publish, error[${err}]`);
93              return;
94          }
95          console.info(`[ANS] publish success`);
96      });
97      ```
98
99      Below is an example of the long-text notification.
100     ![en-us_image_0000001416745530](figures/en-us_image_0000001416745530.png)
101   - In addition to the parameters in the normal text notification, the multi-line text notification provides the **lines**, **briefText**, and **longTitle** parameters. The value of these parameters contains less than 200 bytes. By default, a multi-line notification looks in the same way as a normal text notification. When expanded, the notification displays the title and content specified in **longTitle** and **lines**, respectively.
102
103      ```ts
104      let notificationRequest = {
105        id: 1,
106        content: {
107      	contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // Multi-line text notification
108      	multiLine: {
109      	  title: 'test_title',
110      	  text: 'test_text',
111      	  briefText: 'test_briefText',
112      	  longTitle: 'test_longTitle',
113      	  lines: ['line_01', 'line_02', 'line_03', 'line_04'],
114      	}
115        }
116      }
117
118      // Publish the notification.
119      notificationManager.publish(notificationRequest, (err) => {
120        if (err) {
121      	console.error(`[ANS] failed to publish, error[${err}]`);
122      	return;
123        }
124        console.info(`[ANS] publish success`);
125      });
126      ```
127
128      Below is an example of the multi-line notification.
129     ![en-us_image_0000001417062446](figures/en-us_image_0000001417062446.png)
130   - In addition to the parameters in the normal text notification, the picture-attached text notification provides the **picture**, **briefText**, and **expandedTitle** parameters. The value of **picture** is a pixel map that does not exceed 2 MB.
131
132      ```ts
133      let notificationPicture: PixelMap = undefined; // Obtain the pixel map information.
134      let notificationRequest = {
135          id: 1,
136          content: {
137      	contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
138              picture: {
139                title: 'test_title',
140                text: 'test_text',
141                additionalText: 'test_additionalText',
142                briefText: 'test_briefText',
143                expandedTitle: 'test_expandedTitle',
144                picture: notificationPicture
145              }
146          }
147      }
148
149      // Publish the notification.
150      notificationManager.publish(notificationRequest, (err) => {
151          if (err) {
152      	console.error(`[ANS] failed to publish, error[${err}]`);
153      	return;
154          }
155          console.info(`[ANS] publish success `);
156      });
157      ```
158
159      Below is an example of the picture-attached notification.
160     ![en-us_image_0000001466582045](figures/en-us_image_0000001466582045.png)
161