• 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**Table 1** Basic notification content types
7
8| Type| Description|
9| -------- | -------- |
10| NOTIFICATION_CONTENT_BASIC_TEXT | Normal text notification.|
11| NOTIFICATION_CONTENT_LONG_TEXT | Long text notification.|
12| NOTIFICATION_CONTENT_MULTILINE | Multi-line text notification.|
13| NOTIFICATION_CONTENT_PICTURE | Picture-attached notification.|
14
15Notifications are displayed in the notification panel, which is the only supported subscriber to notifications. Below you can see two examples of the basic notification.
16
17**Figure 1** Examples of the basic notification
18
19![en-us_image_0000001466462305](figures/en-us_image_0000001466462305.png)
20
21
22## Available APIs
23
24The following table describes the APIs for notification publishing. You specify the notification type by setting the [NotificationRequest](../reference/apis/js-apis-inner-notification-notificationRequest.md#notificationrequest) parameter in the APIs.
25
26| Name| Description|
27| -------- | -------- |
28| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | Publishes a notification.                |
29| cancel(id: number, label: string, callback: AsyncCallback<void>): void | Cancels a notification.          |
30| cancelAll(callback: AsyncCallback<void>): void; | Cancels all notifications published by the application.|
31
32
33## How to Develop
34
351. [Enable notification](notification-enable.md). An application can use the notification feature only after being authorized by the user.
36
372. Import the module.
38
39   ```ts
40   import notificationManager from '@ohos.notificationManager';
41   import Base from '@ohos.base';
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: notificationManager.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:Base.BusinessError) => {
61        if (err) {
62          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
63          return;
64        }
65        console.info('Succeeded in publishing notification.');
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: notificationManager.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:Base.BusinessError) => {
91        if (err) {
92          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
93          return;
94        }
95        console.info('Succeeded in publishing notification.');
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: notificationManager.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:Base.BusinessError) => {
120        if (err) {
121          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
122          return;
123        }
124        console.info('Succeeded in publishing notification.');
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 [PixelMap](../reference/apis/js-apis-image.md#pixelmap7) object that does not exceed 2 MB.
131
132      ```ts
133      let imagePixelMap: PixelMap; // Obtain the PixelMap information.
134      let notificationRequest: notificationManager.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: imagePixelMap
145          }
146        }
147      };
148
149      // Publish the notification.
150      notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
151        if (err) {
152          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
153          return;
154        }
155        console.info('Succeeded in publishing notification.');
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