openharmony 鸿蒙 live-view-notification-sys

2026-08-25 浏览 (1)

Publishing a Live View Notification (for System Applications Only)

The live view notification provides real-time progress of long-duration tasks, including voice recording, screen recording, audio and video playback, timing, and phone call. Such type of notification is not stored persistently, and its lifecycle is consistent with that of the notification publisher.

Table 1 Types of live view notifications

TypeValueDescription
NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW11+5Live view notification.

Available APIs

The following table describes the APIs for notification publishing. You can specify the notification information – content, ID, slot type, and delivery time – by setting the NotificationRequest parameter in the APIs. The information can include the notification content, notification ID, notification channel type, and notification sending time.

APIDescription
publish(request: NotificationRequest, callback: AsyncCallback<void>): voidPublishes a notification.
cancel(id: number, label: string, callback: AsyncCallback<void>): voidCancels a notification.

How to Develop

  1. Import the related modules.

    import { notificationManager } from '@kit.NotificationKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { image } from '@kit.ImageKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
    const TAG: string = '[PublishOperation]';
    const DOMAIN_NUMBER: number = 0xFF00;
    
  2. Publish a notification.

    In addition to the parameters in the normal text notification, the system live view notification provides the typeCode, capsule, button, time, and progress parameters. For details, see NotificationSystemLiveViewContent.

    let imagePixelMap: image.PixelMap|undefined = undefined; // Obtain the image pixel map information.
    let color = new ArrayBuffer(4);
    imagePixelMap = await image.createPixelMap(color, {
      size: {
        height: 1,
        width: 1
      }
    })
    if(imagePixelMap !== undefined) {
      let notificationRequest: notificationManager.NotificationRequest = {
        notificationSlotType: notificationManager.SlotType.LIVE_VIEW, // Live view
        id: 0, // Notification ID. The default value is 0.
        content: {
          notificationContentType : notificationManager.ContentType.NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW,
          systemLiveView: {
            title: "test_title",
            text:"test_text",
            typeCode: 1, // Type of the invoking service.
            // Button
            button: {
              names: ["buttonName1"],
              icons: [imagePixelMap],
            },
            // Capsule
            capsule: {
              title: "testTitle",
              icon: imagePixelMap,
              backgroundColor: "testColor",
            },
            // Progress. To update the progress, you only need to modify the progress value and publish the notification again.
            progress: {
              maxValue: 100,
              currentValue: 21,
              isPercentage: false,
            },
            // Time
            time: {
              initialTime: 12,
              isCountDown: true,
              isPaused: true,
              isInTitle: false,
            }
          }
        }
      };
      // publish callback
      let publishCallback = (err: BusinessError): void => {
        if (err) {
          hilog.error(DOMAIN_NUMBER, TAG, `publish failed, code is ${err.code}, message is ${err.message}`);
        } else {
          hilog.info(DOMAIN_NUMBER, TAG, `publish success`);
        }
      };
      // Button callback (It is returned when the button is touched. You can determine how to process the callback.)
      let onResponseCallback = (id:number, option:notificationManager.ButtonOptions) => {
        hilog.info(DOMAIN_NUMBER, TAG, `response callback: ` + JSON.stringify(option) + `notificationId` + id);
      }
      let systemLiveViewSubscriber: notificationManager.SystemLiveViewSubscriber  = {
        onResponse: onResponseCallback
      };
      // Subscribe to the system live view notification (button).
      await notificationManager.subscribeSystemLiveView(systemLiveViewSubscriber);
      // Publish the notification.
      notificationManager.publish(notificationRequest, publishCallback);
    }
    

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 notification-enable

openharmony 鸿蒙 notification-overview

openharmony 鸿蒙 notification-with-wantagent

openharmony 鸿蒙 notification-customized-ringtone

openharmony 鸿蒙 notification-subscriber-extension-ability-development-steps

openharmony 鸿蒙 notification-cancel

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 notification-update

openharmony 鸿蒙 notification-slot

openharmony 鸿蒙 progress-bar-notification

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/7RnFnvxO