[Mid-Autumn Festival and National Day Continuously Updated] HarmonyOS manages notification messages and releases notifications (Part 1)

1. Overview of notification

Notification introduction

The application can send notification messages through the notification interface, and the end user can view the notification content through the notification bar, or click on the notification to open the application.

Common usage scenarios for notifications:

  • Display received short messages, instant messages, etc.
  • Display application push messages, such as advertisements, version updates, etc.
  • Displays currently ongoing events, such as downloads, etc.

HarmonyOS manages notification type messages through ANS (Advanced Notification Service, notification system service) and supports multiple notification types, such as basic type notifications and progress bar type notifications.

Notify business processes

The notification business process consists of a notification subsystem, a notification sending end, and a notification subscribing end.

A notification is generated from the notification sending end, sent to the notification subsystem through IPC communication , and then distributed to the notification subsystem by the notification subsystem.

System applications also support notification-related configurations, such as enable switches and configuration parameters. Requests are initiated from the system configuration and sent to the notification subsystem for storage in memory and database.

  • 2. Publish basic type notifications

Basic type notifications are mainly used to send short messages, reminder messages, advertising push, etc., and support ordinary text types, long text types, multi-line text types and image types.

Table 1  Content classification in basic type notifications

type

describe

NOTIFICATION_CONTENT_BASIC_TEXT

Normal text type.

NOTIFICATION_CONTENT_LONG_TEXT

Long text type.

NOTIFICATION_CONTENT_MULTILINE

Multiline text type.

NOTIFICATION_CONTENT_PICTURE

Image type.

Currently, the system only subscribes to notifications in the notification bar and displays the notifications in the notification bar. The schematic diagram of the basic type notification rendering effect is as follows.

Figure 1  Schematic diagram of basic type notification rendering effect

Interface Description

The notification publishing interface is shown in the following table. Different publishing types of notifications carry different information in the fields of NotificationRequest .

Interface name

describe

publish(request: NotificationRequest, callback: AsyncCallback<void>): void

Post a notice.

cancel(id: number, label: string, callback: AsyncCallback<void>): void

Cancel the specified notification.

cancelAll(callback: AsyncCallback<void>): void;

Dismiss all notifications from this app.

Development steps

  1. Import module.
import NotificationManager from '@ohos.notificationManager';

2. Construct the NotificationRequest object and publish the notification.

  • Ordinary text type notifications consist of three fields: title, text content, and additional information. The title and text content are required fields.
let notificationRequest = {
       
       
  id: 1,
  content: {
       
       
    contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
    normal: {
       
       
      title: 'test_title',
      text: 'test_text',
      additionalText: 'test_additionalText',
    }
  }
}

NotificationManager.publish(notificationRequest, (err) => {
       
       
    if (err) {
       
       
        console.error(`[ANS] failed to publish, error[${err}]`);
        return;
    }
    console.info(`[ANS] publish success`);
});

The running effect is shown in the figure below.

  • The long text type notification inherits the fields of the ordinary text type, and also adds long text content, content summary, and title when the notification is expanded. The default display of the notification is the same as ordinary text. After expansion, the title is displayed as the expanded title content and the content is long text content.
let notificationRequest = {
       
       
  id: 1,
  content: {
       
       
    contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 长文本类型通知
    longText: {
       
       
      title: 'test_title',
      text: 'test_text',
      additionalText: 'test_additionalText',
      longText: 'test_longText',
      briefText: 'test_briefText',
      expandedTitle: 'test_expandedTitle',
    }
  }
}

// 发布通知
NotificationManager.publish(notificationRequest, (err) => {
       
       
    if (err) {
       
       
        console.error(`[ANS] failed to publish, error[${err}]`);
        return;
    }
    console.info(`[ANS] publish success`);
});

The running effect is shown in the figure below.

  • Multi-line text type notifications inherit the fields of ordinary text type, and also add multi-line text content, content summary and title when the notification is expanded. The default display of notifications is the same as ordinary text. After expansion, the title is displayed as the expanded title content, and multi-line text content is displayed in multiple lines.
let notificationRequest = {
       
       
  id: 1,
  content: {
       
       
    contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // 多行文本类型通知
    multiLine: {
       
       
      title: 'test_title',
      text: 'test_text',
      briefText: 'test_briefText',
      longTitle: 'test_longTitle',
      lines: ['line_01', 'line_02', 'line_03', 'line_04'],
    }
  }
}

// 发布通知
NotificationManager.publish(notificationRequest, (err) => {
       
       
  if (err) {
       
       
    console.error(`[ANS] failed to publish, error[${err}]`);
    return;
  }
  console.info(`[ANS] publish success`);
});

The running effect is shown in the figure below.

  • Image type notifications inherit fields of ordinary text type, and also add image content, content summary, and title when the notification is expanded. The image content is a PixelMap object, and its size cannot exceed 2M.
// 图片构造
const color = new ArrayBuffer(60000);
let bufferArr = new Uint8Array(color);
for (var i = 0; i<bufferArr.byteLength;i++) {
       
       
  bufferArr[i++] = 60;
  bufferArr[i++] = 20;
  bufferArr[i++] = 220;
  bufferArr[i] = 100;
}
let opts = {
       
        editable:true, pixelFormat:"ARGB_8888", size: {
       
       height:100, width : 150}};
await image
  .createPixelMap(color, opts)
  .then(async (pixelmap) => {
       
       
    await pixelmap.getImageInfo().then(imageInfo => {
       
       
      console.log("=====size: ====" + JSON.stringify(imageInfo.size));
    }).catch(err => {
       
       
      console.error("Failed to obtain the image pixel map information." + JSON.stringify(err));
      return;
    })
    let notificationRequest = {
       
       
      id: 1,
      content: {
       
       
        contentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE,
        picture: {
       
       
          title: 'test_title',
          text: 'test_text',
          additionalText: 'test_additionalText',
          picture: pixelmap,
          briefText: 'test_briefText',
          expandedTitle: 'test_expandedTitle',
        }
      },
    }
    // 发送通知
    NotificationManager.publish(notificationRequest, (err) => {
       
       
      if (err) {
       
       
        console.error(`[ANS] failed to publish, error[${err}]`);
        return;
      }
      console.info(`[ANS] publish success `);
    });
  }).catch(err=>{
       
       
    console.error('create pixelmap failed =========='+ JSON.stringify(err));
    return;
  })

The running effect is shown in the figure below.

Guess you like

Origin blog.csdn.net/HarmonyOSDev/article/details/133313046