1# Requesting Notification Authorization 2 3Your application can send notifications only after obtaining user authorization. Before publishing a notification, the application should call the [requestEnableNotification()](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerrequestenablenotification10-1) API to display a dialog box for the user to determine whether to allow notification sending. If the user rejects the authorization, the dialog box cannot be displayed again. To request another notification authorization from the user, the application can call the [openNotificationSettings](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanageropennotificationsettings13) API to display the semi-modal dialog box for notification management. 4 5## Available APIs 6 7For details about the APIs, see [@ohos.notificationManager (NotificationManager)](../reference/apis-notification-kit/js-apis-notificationManager.md). 8 9**Table 1** Notification authorization APIs 10 11| **API** | **Description**| 12| -------- | -------- | 13| isNotificationEnabled():Promise\<boolean\> | Checks whether notification is enabled. | 14| 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. | 15| openNotificationSettings(context: UIAbilityContext): Promise\<void\> | Opens a dialog box for notification management.| 16 17 18## How to Develop 19 201. Import the **NotificationManager** module. 21 22 ```ts 23 import { notificationManager } from '@kit.NotificationKit'; 24 import { BusinessError } from '@kit.BasicServicesKit'; 25 import { hilog } from '@kit.PerformanceAnalysisKit'; 26 import { common } from '@kit.AbilityKit'; 27 28 const TAG: string = '[PublishOperation]'; 29 const DOMAIN_NUMBER: number = 0xFF00; 30 ``` 31 322. Display a dialog box to request notification authorization from the user. 33 34 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. 35 36 ```ts 37 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 38 notificationManager.isNotificationEnabled().then((data: boolean) => { 39 hilog.info(DOMAIN_NUMBER, TAG, "isNotificationEnabled success, data: " + JSON.stringify(data)); 40 if(!data){ 41 notificationManager.requestEnableNotification(context).then(() => { 42 hilog.info(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification success`); 43 }).catch((err : BusinessError) => { 44 if(1600004 == err.code){ 45 hilog.error(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification refused, code is ${err.code}, message is ${err.message}`); 46 } else { 47 hilog.error(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`); 48 } 49 }); 50 } 51 }).catch((err : BusinessError) => { 52 hilog.error(DOMAIN_NUMBER, TAG, `isNotificationEnabled fail, code is ${err.code}, message is ${err.message}`); 53 }); 54 ``` 55 563. (Optional) Display a semi-modal dialog box to request notification authorization from the user again. 57 58 ```ts 59 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 60 notificationManager.isNotificationEnabled().then((data: boolean) => { 61 hilog.info(DOMAIN_NUMBER, TAG, "isNotificationEnabled success, data: " + JSON.stringify(data)); 62 if(!data){ 63 notificationManager.openNotificationSettings(context).then(() => { 64 hilog.info(0x0000, 'testTag', `[ANS] openNotificationSettings success`); 65 }).catch((err: BusinessError) => { 66 hilog.error(0x0000, 'testTag', `[ANS] openNotificationSettings failed, code is ${err.code}, message is ${err.message}`); 67 }); 68 } 69 }) 70 ``` 71