Requesting Notification Authorization

Your application can send notifications only after obtaining user authorization. You can call the requestEnableNotification() 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.

Figure 1 Dialog box prompting the user to enable notification en-us_image_0000001416585590

  • Touching allow enables notification for the application.

  • Touching ban keeps notification disabled. In this case, since another call to requestEnableNotification() will not display the dialog box again, the user will need to perform the following to manually enable notification.

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.
en-us_image_0000001417062434 en-us_image_0000001466462297 en-us_image_0000001466782025

Available APIs

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

Table 1 Notification authorization APIs

Name Description
requestEnableNotification(callback: AsyncCallback<void>): void Requests notification to be enabled. When called for the first time, this API displays a dialog box prompting the user to enable notification.

How to Develop

  1. Import the NotificationManager module.

    import notificationManager from '@ohos.notificationManager';
    import Base from '@ohos.base';
    
  2. Request notification to be enabled.

    notificationManager.requestEnableNotification().then(() => {
      console.info(`[ANS] requestEnableNotification success`);
    }).catch((err: Base.BusinessError) => {
      console.error(`[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`);
    });