harmony 鸿蒙Publishing a Progress Notification

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

Publishing a Progress Notification

The progress notification is a commonly used notification type, mainly used to display the progress of an ongoing operation, such as file downloading. When publishing a progress notification through the notification subsystem, you can use the readily available template by specifying the related attributes, such as the template name and template data.

In the NotificationTemplate, which can only be of the progress type, data indicates custom template data.

Available APIs

Name Description
isSupportTemplate(templateName: string, callback: AsyncCallback<boolean>): void Checks whether a specific template is supported. This API uses an asynchronous callback to return the result. For details, see isSupportTemplate().
Only the progress-type template is supported.

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. Check whether a specific template is supported. In this example, the template of the downloadTemplate type is checked.
   notificationManager.isSupportTemplate('downloadTemplate').then((data:boolean) => {
     console.info(`[ANS] isSupportTemplate success`);
     console.info('Succeeded in supporting download template notification.');
     let isSupportTpl: boolean = data; // The value true means that the template of the downloadTemplate type is supported, and false means the opposite.
     // ...
   }).catch((err:Base.BusinessError) => {
     console.error(`Failed to support download template notification. Code is ${err.code}, message is ${err.message}`);
   });

NOTE

Proceed with the step below only when the specified template is supported.

  1. Create a NotificationRequest object and publish a progress notification.
   let notificationRequest: notificationManager.NotificationRequest = {
     id: 1,
     content: {
       contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
       normal: {
         title: 'test_title',
         text: 'test_text',
         additionalText: 'test_additionalText'
       }
     },
     // Create a progress template. The name field has a fixed value of downloadTemplate.
     template: {
       name: 'downloadTemplate',
       data: { title: 'File Title', fileName: 'music.mp4', progressValue: 45 }
     }
   }
   
   // 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.');
   });

你可能感兴趣的鸿蒙文章

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 Basic Notification

0  赞