harmony 鸿蒙Enabling Notification

  • 2023-02-03
  • 浏览 (765)

Enabling Notification

To publish a notification, you must have notification enabled for your application. You can call the requestEnableNotification() API to display a dialog box prompting the user to enable notification for your application. Note that 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, and touching ban keeps notification disabled.

  • The dialog box will not be displayed again when requestEnableNotification() is called later. The user can manually enable notification as follows.

1. Swipe down from the upper left corner of the device screen to access the notification panel. 2. Touch the Settings icon in the upper right corner. On the notification screen, locate 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.

Table 1 Notification APIs

Name Description
isNotificationEnabled(bundle: BundleOption, callback: AsyncCallback<boolean>): void Checks whether notification is enabled.
NOTE
This is a system API and cannot be called by third-party applications.
setNotificationEnable(bundle: BundleOption, enable: boolean, callback: AsyncCallback<void>): void Sets whether to enable notification. Notification can be enabled or disabled in Notifications of the target application under Settings > Apps & services > Apps.
NOTE
This is a system API and cannot be called by third-party applications.
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';
  1. Call the API to 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}`);
   });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Notification

harmony 鸿蒙Managing the Notification Badge

harmony 鸿蒙Notification Overview

harmony 鸿蒙Subscribing to Notifications (for System Applications Only)

harmony 鸿蒙Adding a WantAgent Object to a Notification

harmony 鸿蒙Publishing a Progress Notification

harmony 鸿蒙Publishing a Basic Notification

0  赞