openharmony 鸿蒙 notification-slot

2025-06-12 浏览 (1)

Managing Notification Slots

The system supports a range of notification slots. Different notification slots are assigned different reminder modes. You can choose notification slots for your application and manage them as required, such as creating, querying, and deleting notification slots.

Notification Slots

The following table lists the notification slots and their reminder modes. Y indicates that the feature is supported, and N indicates that the feature is not supported.

SlotTypeValueCategoryNotification PanelBannerLock ScreenAlert Tone/VibrationStatus Bar IconAutomatic Screen-on
UNKNOWN_TYPE0UnknownYNNNNN
SOCIAL_COMMUNICATION1Social communicationYYYYYY
SERVICE_INFORMATION2Service notificationYYYYYY
CONTENT_INFORMATION3Content and newsYNNNNN
CUSTOMER_SERVICE5Customer serviceYNNYYN
OTHER_TYPES0xFFFFOtherYNNNNN

Available APIs

The main notification slot APIs are as follows. For details about other APIs, see @ohos.notificationManager (NotificationManager).

APIDescription
addSlot(type: SlotType): Promise<void>Adds a notification slot.
getSlot(slotType: SlotType): Promise<NotificationSlot>Obtains a notification slot.
removeSlot(slotType: SlotType): Promise<void>Removes a notification slot for this application.

In addition to using addslot(), you can also create a notification slot by passing notificationSlotType in the NotificationRequest. If the specified notification slot does not exist, it is automatically created.

How to Develop

  1. Import the notificationManager module.

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

    // addSlot callback
    let addSlotCallBack = (err: BusinessError): void => {
      if (err) {
        hilog.error(DOMAIN_NUMBER, TAG, `addSlot failed, code is ${err.code}, message is ${err.message}`);
      } else {
        hilog.info(DOMAIN_NUMBER, TAG, `addSlot success`);
      }
    }
    notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION, addSlotCallBack);
    
  3. Obtain a notification slot.

    Obtain whether the corresponding slot is created and the notification modes supported by the slot, for example, whether there is an alert tone, whether there is vibration, and whether the lock screen is visible.

    // getSlot callback
    let getSlotCallback = (err: BusinessError, data: notificationManager.NotificationSlot): void => {
      if (err) {
        hilog.error(DOMAIN_NUMBER, TAG, `Failed to get slot. Code is ${err.code}, message is ${err.message}`);
      } else {
        hilog.info(DOMAIN_NUMBER, TAG, `Succeeded in getting slot.`);
        if (data != null) {
          hilog.info(DOMAIN_NUMBER, TAG, `slot enable status is ${JSON.stringify(data.enabled)}`);
          hilog.info(DOMAIN_NUMBER, TAG, `slot level is ${JSON.stringify(data.level)}`);
          hilog.info(DOMAIN_NUMBER, TAG, `vibrationEnabled status is ${JSON.stringify(data.vibrationEnabled)}`);
          hilog.info(DOMAIN_NUMBER, TAG, `lightEnabled status is ${JSON.stringify(data.lightEnabled)}`);
        }
      }
    }
    let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
    notificationManager.getSlot(slotType, getSlotCallback);
    
  4. Remove a notification slot.

    // removeSlot callback
    let removeSlotCallback = (err: BusinessError): void => {
      if (err) {
        hilog.error(DOMAIN_NUMBER, TAG, `removeSlot failed, code is ${JSON.stringify(err.code)}, message is ${JSON.stringify(err.message)}`);
      } else {
        hilog.info(DOMAIN_NUMBER, TAG, "removeSlot success");
      }
    }
    let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
    notificationManager.removeSlot(slotType, removeSlotCallback);
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Notification Kit (User Notification Service)

harmony 鸿蒙Publishing a Live View Notification (for System Applications Only)

harmony 鸿蒙Managing the Notification Badge

harmony 鸿蒙Canceling a Notification

harmony 鸿蒙Clearing Repeated Notifications Across Devices

harmony 鸿蒙Cross-Device Notification Management (for System Applications Only)

harmony 鸿蒙Cross-Device Notification Overview

harmony 鸿蒙Requesting Notification Authorization

harmony 鸿蒙Introduction to Notification Kit

harmony 鸿蒙Enabling Quick Reply for Cross-device Notifications

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