Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.
HarmonyOS
An app must obtain user authorization before it can send notifications. Before publishing a notification, call requestEnableNotification() to display a dialog box for the user to choose whether to allow notifications. After the user denies authorization, the dialog box cannot be displayed again through this API. If the app needs to request notification authorization from the user again, it can use openNotificationSettingsWithResult to display a semi-modal dialog for notification management.
For details about the APIs, see @ohos.notificationManager (NotificationManager).
Table 1 Notification authorization API description
| API | Description |
|---|---|
| isNotificationEnabled():Promise<boolean> | Checks whether notification is enabled. |
| requestEnableNotification(context: UIAbilityContext): Promise<void> | Requests notification to be enabled. When called for the first time, this API displays a dialog box prompting the user to select. |
| openNotificationSettingsWithResult(context: UIAbilityContext): Promise<NotificationSetting> | Opens the notification management dialog and returns the setting result after the user completes the settings. |
Import the NotificationManager module.
- import { notificationManager } from '@kit.NotificationKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { common } from '@kit.AbilityKit';
-
- const TAG: string = '[PublishOperation]';
- const DOMAIN_NUMBER: number = 0xFF00;
Display the notification dialog box to request notification authorization from the user.
You can determine whether the user has authorized the request based on the error code of requestEnableNotification. If the error code 1600004 is returned, the authorization is rejected.
- let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
- notificationManager.isNotificationEnabled().then((data: boolean) => {
- hilog.info(DOMAIN_NUMBER, TAG, `isNotificationEnabled success, data: ${data}` );
- if (!data) {
- notificationManager.requestEnableNotification(context).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification success`);
- }).catch((err: BusinessError) => {
- if (1600004 == err.code) {
- hilog.error(DOMAIN_NUMBER, TAG,
- `[ANS] requestEnableNotification refused, code is ${err.code}, message is ${err.message}`);
- } else {
- hilog.error(DOMAIN_NUMBER, TAG,
- `[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`);
- }
- });
- }
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG,
- `isNotificationEnabled fail, code is ${err.code}, message is ${err.message}`);
- });
(Optional) Display a semi-modal dialog box to request notification authorization from the user again.
After the user completes authorization, the setting result is returned, which includes the notification authorization switch and the setting results of the lock screen, banner, badge, ringtone, and vibration switches.
- let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
- notificationManager.isNotificationEnabled().then((data: boolean) => {
- hilog.info(DOMAIN_NUMBER, TAG, `isNotificationEnabled success, data: ${data}`);
- if (!data) {
- notificationManager.openNotificationSettingsWithResult(context)
- .then((result: notificationManager.NotificationSetting) => {
- // result indicates the current setting result.
- hilog.info(DOMAIN_NUMBER, TAG, `[ANS] openNotificationSettingsWithResult success, result: ${JSON.stringify(result)}`);
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG,
- `[ANS] openNotificationSettingsWithResult failed, code is ${err.code}, message is ${err.message}`);
- });
- }
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG,
- `isNotificationEnabled fail, code is ${err.code}, message is ${err.message}`);
- });
Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick start
Helps you find desired resources with ease.