目录

Managing Notification Slots

The system supports multiple notification slots. Different notification slots correspond to different notification reminder modes. You can select an appropriate notification channel based on the actual application scenario and manage notification slots (including creating, querying, and deleting them).

Notification Slots

Different types of notification slots correspond to different notification reminder modes, as shown in the following table. In the table, Y indicates supported and N indicates not supported.

Users can go to Settings > Notifications & status bar, select the corresponding app, and manage its notification slots. When Allow notifications is enabled for an app, banner notifications are disabled by default and cannot be configured by the app, but can be manually enabled by the user. Lock screen notifications, home screen badges, sound, vibration, and other notification methods are enabled by default.

The actual notification appearance depends on device capabilities and the notification center UI style. For details, see Notification Scenarios.

SlotType Value Category Push Message Classification Notification Panel Banner Lock Screen Alert Tone/Vibration Status Bar Icon Automatic Screen-on
SOCIAL_COMMUNICATION 1 Social communication

IM

VOIP

MISS_CALL

Y Y Y Y Y Y
SERVICE_INFORMATION 2 Service notification

TRAVEL

HEALTH

WORK

ACCOUNT

EXPRESS

FINANCE

DEVICE_REMINDER

MAIL

PLAY_VOICE

SUBSCRIPTION

Y Y Y Y Y Y
CUSTOMER_SERVICE 5 Customer service -- Y N N Y Y N
CONTENT_INFORMATION 3 Content and news MARKETING Y N N N N N
UNKNOWN_TYPE 0 Unknown MARKETING Y N N N N N
OTHER_TYPES 0xFFFF Other MARKETING Y N N N N N

Available APIs

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

API Description
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. Create a notification slot of the specified type.

    // 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 has been created and the notification reminder modes supported by the slot, such as whether a ringtone is available, whether vibration is available, and whether the notification is visible on the lock screen.

    // 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, `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);