harmony 鸿蒙Publishing a Basic Notification

  • 2023-02-03
  • 浏览 (610)

Publishing a Basic Notification

You 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.

Table 1 Basic notification content types

Type Description
NOTIFICATION_CONTENT_BASIC_TEXT Normal text notification.
NOTIFICATION_CONTENT_LONG_TEXT Long text notification.
NOTIFICATION_CONTENT_MULTILINE Multi-line text notification.
NOTIFICATION_CONTENT_PICTURE Picture-attached notification.

Notifications are displayed in the notification panel, which is the only supported subscriber to notifications. Below you can see two examples of the basic notification.

Figure 1 Examples of the basic notification

en-us_image_0000001466462305

Available APIs

The following table describes the APIs for notification publishing. You specify the notification type by setting the NotificationRequest parameter in the APIs.

Name Description
publish(request: NotificationRequest, callback: AsyncCallback<void>): void Publishes a notification.
cancel(id: number, label: string, callback: AsyncCallback<void>): void Cancels a notification.
cancelAll(callback: AsyncCallback<void>): void; Cancels all notifications published by the application.

How to Develop

  1. Enable notification. An application can use the notification feature only after being authorized by the user.

  2. Import the module.

   import notificationManager from '@ohos.notificationManager';
   import Base from '@ohos.base';
  1. Create a NotificationRequest object and publish a progress notification.

    • 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.
      let notificationRequest: notificationManager.NotificationRequest = {
        id: 1,
        content: {
          contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // Basic notification
          normal: {
            title: 'test_title',
            text: 'test_text',
            additionalText: 'test_additionalText',
          }
        }
      };
    
    
      notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
        if (err) {
          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
          return;
        }
        console.info('Succeeded in publishing notification.');
      });
    

    Below is an example of the normal text notification. en-us_image_0000001466782033 - 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.

      let notificationRequest: notificationManager.NotificationRequest = {
        id: 1,
        content: {
          contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // Long-text notification
          longText: {
            title: 'test_title',
            text: 'test_text',
            additionalText: 'test_additionalText',
            longText: 'test_longText',
            briefText: 'test_briefText',
            expandedTitle: 'test_expandedTitle',
          }
        }
      };
    
    
      // Publish the notification.
      notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
        if (err) {
          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
          return;
        }
        console.info('Succeeded in publishing notification.');
      });
    

    Below is an example of the long-text notification. en-us_image_0000001416745530 - 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.

      let notificationRequest: notificationManager.NotificationRequest = {
        id: 1,
        content: {
          contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // Multi-line text notification
          multiLine: {
            title: 'test_title',
            text: 'test_text',
            briefText: 'test_briefText',
            longTitle: 'test_longTitle',
            lines: ['line_01', 'line_02', 'line_03', 'line_04'],
          }
        }
      };
    
    
      // Publish the notification.
      notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
        if (err) {
          console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
          return;
        }
        console.info('Succeeded in publishing notification.');
      });
    

    Below is an example of the multi-line notification. en-us_image_0000001417062446 - 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 object that does not exceed 2 MB.

      import image from '@ohos.multimedia.image';
    
    
      let imagePixelMap: image.PixelMap|undefined = undefined; // Obtain the PixelMap information.
      let color = new ArrayBuffer(0);
      image.createPixelMap(color, {
        size: {
          height: 0,
          width: 0
        }
      }).then((data: image.PixelMap) => {
        imagePixelMap = data;
      }).catch((err: Base.BusinessError) => {
        console.log(`createPixelMap failed, error: ${err}`);
      })
    
    
      if (imagePixelMap !== undefined) {
        let notificationRequest: notificationManager.NotificationRequest = {
          id: 1,
          content: {
            contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
            picture: {
              title: 'test_title',
              text: 'test_text',
              additionalText: 'test_additionalText',
              briefText: 'test_briefText',
              expandedTitle: 'test_expandedTitle',
              picture: imagePixelMap
            }
          }
        };
    
    
        // Publish the notification.
        notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
          if (err) {
            console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
            return;
          }
          console.info('Succeeded in publishing notification.');
        });
      }
    

    Below is an example of the picture-attached notification. en-us_image_0000001466582045

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Notification

harmony 鸿蒙Managing the Notification Badge

harmony 鸿蒙Enabling Notification

harmony 鸿蒙Notification Overview

harmony 鸿蒙Subscribing to Notifications (for System Applications Only)

harmony 鸿蒙Adding a WantAgent Object to a Notification

harmony 鸿蒙Publishing a Progress Notification

0  赞