• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Publishing a Text Notification
2
3You can publish text notifications to send SMS messages, alert messages, and more. There are two types of text notifications: normal text and multi-line text.
4
5**Table 1** Basic notification content types
6
7| Type                            | Description         |
8| ------------------------------- | ------------- |
9| NOTIFICATION_CONTENT_BASIC_TEXT | Normal text notification.|
10| NOTIFICATION_CONTENT_MULTILINE  | Multi-line text notification.|
11
12## Available APIs
13
14The following table describes the APIs for notification publishing. You specify the notification information – content, ID, slot type, and publish time – by setting the [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest-1) parameter in the APIs.
15
16| Name| Description|
17| -------- | -------- |
18| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | Publishes a notification.                |
19
20
21## How to Develop
22
231. Import the module.
24
25   ```ts
26   import { notificationManager } from '@kit.NotificationKit';
27   import { BusinessError } from '@kit.BasicServicesKit';
28   import { hilog } from '@kit.PerformanceAnalysisKit';
29
30   const TAG: string = '[PublishOperation]';
31   const DOMAIN_NUMBER: number = 0xFF00;
32   ```
33
342. Create a **NotificationRequest** object and publish a progress notification.
35   - A normal text notification consists of the **title**, **text**, and **additionalText** fields. For details, see [NotificationBasicContent](../reference/apis-notification-kit/js-apis-inner-notification-notificationContent.md#notificationbasiccontent).
36
37      ```ts
38      let notificationRequest: notificationManager.NotificationRequest = {
39        id: 1,
40        content: {
41          notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // Basic notification
42          normal: {
43            title: 'test_title',
44            text: 'test_text',
45            additionalText: 'test_additionalText',
46          }
47        }
48      };
49      notificationManager.publish(notificationRequest, (err: BusinessError) => {
50        if (err) {
51          hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
52          return;
53        }
54        hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in publishing notification.');
55      });
56      ```
57
58
59   - In addition to the fields in the normal text notification, the multi-line text notification provides the **lines**, **briefText**, and **longTitle** fields. For details, see [NotificationMultiLineContent](../reference/apis-notification-kit/js-apis-inner-notification-notificationContent.md#notificationmultilinecontent).
60
61      ```ts
62      let notificationRequest: notificationManager.NotificationRequest = {
63        id: 3,
64        content: {
65          notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // Multi-line text notification
66          multiLine: {
67            title: 'test_title',
68            text: 'test_text',
69            briefText: 'test_briefText',
70            longTitle: 'test_longTitle',
71            lines: ['line_01', 'line_02', 'line_03'],
72          }
73        }
74      };
75      // Publish the notification.
76      notificationManager.publish(notificationRequest, (err: BusinessError) => {
77        if (err) {
78          hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
79          return;
80        }
81        hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in publishing notification.');
82      });
83      ```
84