@ohos.intelligentScene (Intelligent Scene)
This module provides APIs for querying the DND mode status, including whether the system DND mode is enabled, whether the application is allowed to be disturbed, and so on. When a specific intelligent scene (DND mode, sleep mode, study mode, work mode, or custom mode) is enabled, the DND mode is enabled.
NOTE
- The initial APIs of this module are supported since API version 23. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Modules to Import
import { intelligentScene } from '@kit.BasicServicesKit';
intelligentScene.isDoNotDisturbEnabled
isDoNotDisturbEnabled(): Promise<boolean>
Checks whether the system DND mode is enabled. This API uses a promise to return the result.
Model constraint: This API can be used only in the stage model.
System capability: SystemCapability.Applications.IntelligentScene
Required permissions: ohos.permission.GET_DONOTDISTURB_STATE
Return value
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result, indicating whether the DND mode is enabled. The value true indicates that the DND mode is enabled, and false indicates the opposite. |
Error codes
For details about the error codes, see Intelligent Scene Error Codes and Universal Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 35200001 | Internal error. |
Example
import { BusinessError, intelligentScene } from '@kit.BasicServicesKit';
let isDoNotDisturbEnabled: boolean = false;
try {
isDoNotDisturbEnabled = await intelligentScene.isDoNotDisturbEnabled();
} catch (err) {
console.error(`Failed to get doNotDisturb state, code: ${err.code}, message: ${err.message}`);
}
if (isDoNotDisturbEnabled) {
console.info('DoNotDisturb state is open');
} else {
console.info('DoNotDisturb state is closed');
}
intelligentScene.isNotifyAllowedInDoNotDisturb
isNotifyAllowedInDoNotDisturb(): Promise<boolean>
Checks whether this application is allowed to push notifications in DND mode. If the current application has been added to the allowlist, notifications will be properly pushed with ringtone and vibration in DND mode. This API uses a promise to return the result.
Model constraint: This API can be used only in the stage model.
System capability: SystemCapability.Applications.IntelligentScene
Required permissions: ohos.permission.GET_DONOTDISTURB_STATE
Return value
| Type | Description |
|---|---|
| Promise<boolean> | Promise used to return the result. If the DND mode is disabled, the value false is returned. If the DND mode is enabled, the check result is returned, where true indicates that this application is allowed to push notifications, and false indicates the opposite. |
Error codes
For details about the error codes, see Intelligent Scene Error Codes and Universal Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 35200001 | Internal error. |
Example
import { BusinessError, intelligentScene } from '@kit.BasicServicesKit';
let isNotifyAllowedInDoNotDisturb: boolean = false;
try {
isNotifyAllowedInDoNotDisturb = await intelligentScene.isNotifyAllowedInDoNotDisturb();
} catch (err) {
console.error(`Failed to get doNotDisturb state, code: ${err.code}, message: ${err.message}`);
}
if (isNotifyAllowedInDoNotDisturb) {
console.info('Allowed to notify in doNotDisturb state');
} else {
console.info('Not allowed to notify in doNotDisturb state or doNotDisturb is closed');
}