• 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> **NOTE**
17>
18> The figures are for reference only. The actual effect may vary.
19
20**Figure 1** Examples of the basic notification
21
22![en-us_image_0000001466462305](figures/en-us_image_0000001466462305.png)
23
24
25## Available APIs
26
27The 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.
28
29| Name| Description|
30| -------- | -------- |
31| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | Publishes a notification.                |
32| cancel(id: number, label: string, callback: AsyncCallback<void>): void | Cancels a notification.          |
33| cancelAll(callback: AsyncCallback<void>): void; | Cancels all notifications published by the application.|
34
35
36## How to Develop
37
381. [Request notification authorization](notification-enable.md). Your application can send notifications only after obtaining user authorization.
39
402. Import the module.
41
42   ```ts
43   import notificationManager from '@ohos.notificationManager';
44   import Base from '@ohos.base';
45   ```
46
473. Create a **NotificationRequest** object and publish a progress notification.
48   - 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.
49
50      ```ts
51      let notificationRequest: notificationManager.NotificationRequest = {
52        id: 1,
53        content: {
54          contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // Basic notification
55          normal: {
56            title: 'test_title',
57            text: 'test_text',
58            additionalText: 'test_additionalText',
59          }
60        }
61      };
62
63      notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
64        if (err) {
65          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
66          return;
67        }
68        console.info('Succeeded in publishing notification.');
69      });
70      ```
71
72      Below is an example of the normal text notification.
73     ![en-us_image_0000001466782033](figures/en-us_image_0000001466782033.png)
74   - 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.
75
76      ```ts
77      let notificationRequest: notificationManager.NotificationRequest = {
78        id: 2,
79        content: {
80          contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // Long-text notification
81          longText: {
82            title: 'test_title',
83            text: 'test_text',
84            additionalText: 'test_additionalText',
85            longText: 'test_longText',
86            briefText: 'test_briefText',
87            expandedTitle: 'test_expandedTitle',
88          }
89        }
90      };
91
92      // Publish the notification.
93      notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
94        if (err) {
95          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
96          return;
97        }
98        console.info('Succeeded in publishing notification.');
99      });
100      ```
101
102      Below is an example of the long-text notification.
103     ![en-us_image_0000001416745530](figures/en-us_image_0000001416745530.png)
104   - 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.
105
106      ```ts
107      let notificationRequest: notificationManager.NotificationRequest = {
108        id: 3,
109        content: {
110          contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // Multi-line text notification
111          multiLine: {
112            title: 'test_title',
113            text: 'test_text',
114            briefText: 'test_briefText',
115            longTitle: 'test_longTitle',
116            lines: ['line_01', 'line_02', 'line_03', 'line_04'],
117          }
118        }
119      };
120
121      // Publish the notification.
122      notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
123        if (err) {
124          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
125          return;
126        }
127        console.info('Succeeded in publishing notification.');
128      });
129      ```
130
131      Below is an example of the multi-line notification.
132     ![en-us_image_0000001417062446](figures/en-us_image_0000001417062446.png)
133   - 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.
134
135      ```ts
136      import image from '@ohos.multimedia.image';
137
138      let imagePixelMap: image.PixelMap | undefined = undefined; // The image pixel map information needs to be obtained.
139      let color = new ArrayBuffer(4);
140      image.createPixelMap(color, {
141        size: {
142          height: 1,
143          width: 1
144        }
145      }).then((data: image.PixelMap) => {
146        imagePixelMap = data;
147      }).catch((err: Base.BusinessError) => {
148        console.log(`createPixelMap failed, error: ${err}`);
149      })
150
151      if (imagePixelMap !== undefined) {
152        let notificationRequest: notificationManager.NotificationRequest = {
153          id: 4,
154          content: {
155            contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
156            picture: {
157              title: 'test_title',
158              text: 'test_text',
159              additionalText: 'test_additionalText',
160              briefText: 'test_briefText',
161              expandedTitle: 'test_expandedTitle',
162              picture: imagePixelMap
163            }
164          }
165        };
166
167        // Publish the notification.
168        notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
169          if (err) {
170            console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
171            return;
172          }
173          console.info('Succeeded in publishing notification.');
174        });
175      }
176      ```
177
178      Below is an example of the picture-attached notification.
179     ![en-us_image_0000001466582045](figures/en-us_image_0000001466582045.png)
180