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.

Only Essential Cookies
Accept All
GuidesApplication ServicesNotification KitRequesting Notification Authorization

Requesting Notification Authorization

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.

Available APIs

For details about the APIs, see @ohos.notificationManager (NotificationManager).

Table 1 Notification authorization API description

Expand
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.

How to Develop

  1. Import the NotificationManager module.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { notificationManager } from '@kit.NotificationKit';
    2. import { BusinessError } from '@kit.BasicServicesKit';
    3. import { hilog } from '@kit.PerformanceAnalysisKit';
    4. import { common } from '@kit.AbilityKit';
    5. const TAG: string = '[PublishOperation]';
    6. const DOMAIN_NUMBER: number = 0xFF00;
  2. 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.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
    2. notificationManager.isNotificationEnabled().then((data: boolean) => {
    3. hilog.info(DOMAIN_NUMBER, TAG, `isNotificationEnabled success, data: ${data}` );
    4. if (!data) {
    5. notificationManager.requestEnableNotification(context).then(() => {
    6. hilog.info(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification success`);
    7. }).catch((err: BusinessError) => {
    8. if (1600004 == err.code) {
    9. hilog.error(DOMAIN_NUMBER, TAG,
    10. `[ANS] requestEnableNotification refused, code is ${err.code}, message is ${err.message}`);
    11. } else {
    12. hilog.error(DOMAIN_NUMBER, TAG,
    13. `[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`);
    14. }
    15. });
    16. }
    17. }).catch((err: BusinessError) => {
    18. hilog.error(DOMAIN_NUMBER, TAG,
    19. `isNotificationEnabled fail, code is ${err.code}, message is ${err.message}`);
    20. });
  3. (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.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
    2. notificationManager.isNotificationEnabled().then((data: boolean) => {
    3. hilog.info(DOMAIN_NUMBER, TAG, `isNotificationEnabled success, data: ${data}`);
    4. if (!data) {
    5. notificationManager.openNotificationSettingsWithResult(context)
    6. .then((result: notificationManager.NotificationSetting) => {
    7. // result indicates the current setting result.
    8. hilog.info(DOMAIN_NUMBER, TAG, `[ANS] openNotificationSettingsWithResult success, result: ${JSON.stringify(result)}`);
    9. }).catch((err: BusinessError) => {
    10. hilog.error(DOMAIN_NUMBER, TAG,
    11. `[ANS] openNotificationSettingsWithResult failed, code is ${err.code}, message is ${err.message}`);
    12. });
    13. }
    14. }).catch((err: BusinessError) => {
    15. hilog.error(DOMAIN_NUMBER, TAG,
    16. `isNotificationEnabled fail, code is ${err.code}, message is ${err.message}`);
    17. });