• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Requesting Notification Authorization
2
3
4Your application can send notifications only after obtaining user authorization. You can call the [requestEnableNotification()](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerrequestenablenotification) API to display a dialog box prompting the user to enable notification for your application. The dialog box is displayed only when the API is called for the first time.
5
6  **Figure 1** Dialog box prompting the user to enable notification
7![en-us_image_0000001416585590](figures/en-us_image_0000001416585590.png)
8
9- Touching **allow** enables notification for the application.
10
11- Touching **ban** keeps notification disabled. In this case, since another call to [requestEnableNotification()](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerrequestenablenotification) will not display the dialog box again, the user will need to perform the following to manually enable notification.
12
13  | 1. Swipe down from the upper left corner of the screen and touch the Settings icon in the upper right corner.                             | 2. Access the notification screen and find the target application.| 3. Toggle on **Allow notifications**.                                         |
14  | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
15  | ![en-us_image_0000001417062434](figures/en-us_image_0000001417062434.png) | ![en-us_image_0000001466462297](figures/en-us_image_0000001466462297.png) | ![en-us_image_0000001466782025](figures/en-us_image_0000001466782025.png) |
16
17
18## Available APIs
19
20For details about the APIs, see [@ohos.notificationManager (NotificationManager)](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerrequestenablenotification).
21
22**Table 1** Notification authorization APIs
23
24| Name | Description |
25| -------- | -------- |
26| isNotificationEnabled():Promise\<boolean\>       | Checks whether notification is enabled. |
27| requestEnableNotification(callback: AsyncCallback&lt;void&gt;): void | Requests notification to be enabled. When called for the first time, this API displays a dialog box prompting the user to enable notification.    |
28
29
30## How to Develop
31
321. Import the **NotificationManager** module.
33
34    ```ts
35    import notificationManager from '@ohos.notificationManager';
36    import Base from '@ohos.base';
37    ```
38
392. Request notification to be enabled.
40
41    ```ts
42    notificationManager.isNotificationEnabled().then((data: boolean) => {
43      console.info("isNotificationEnabled success, data: " + JSON.stringify(data));
44      if(!data){
45        notificationManager.requestEnableNotification().then(() => {
46          console.info(`[ANS] requestEnableNotification success`);
47        }).catch((err:Base.BusinessError) => {
48          console.error(`[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`);
49        });
50      }
51    }).catch((err: Base.BusinessError) => {
52      console.error(`isNotificationEnabled fail: ${JSON.stringify(err)}`);
53    });
54    ```
55