harmony 鸿蒙Requesting Notification Authorization
Requesting Notification Authorization
Your application can send notifications only after obtaining user authorization. Call requestEnableNotification() before a notification is published. A dialog box is displayed for the user to determine whether to allow notification sending. When this API is called again, no dialog box is displayed.
Available APIs
For details about the APIs, see @ohos.notificationManager (NotificationManager).
Table 1 Notification authorization APIs
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. |
How to Develop
Import the NotificationManager module.
import { notificationManager } from '@kit.NotificationKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { common } from '@kit.AbilityKit'; const TAG: string = '[PublishOperation]'; const DOMAIN_NUMBER: number = 0xFF00;
Request notification to be enabled.
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.
let context = this.getUIContext().getHostContext() as common.UIAbilityContext; notificationManager.isNotificationEnabled().then((data: boolean) => { hilog.info(DOMAIN_NUMBER, TAG, "isNotificationEnabled success, data: " + JSON.stringify(data)); if(!data){ notificationManager.requestEnableNotification(context).then(() => { hilog.info(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification success`); }).catch((err : BusinessError) => { if(1600004 == err.code){ hilog.error(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification refused, code is ${err.code}, message is ${err.message}`); } else { hilog.error(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`); } }); } }).catch((err : BusinessError) => { hilog.error(DOMAIN_NUMBER, TAG, `isNotificationEnabled fail, code is ${err.code}, message is ${err.message}`); });
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Notification Kit (User Notification Service)
harmony 鸿蒙Publishing a Live View Notification (for System Applications Only)
harmony 鸿蒙Managing the Notification Badge
harmony 鸿蒙Canceling a Notification
harmony 鸿蒙Clearing Repeated Notifications Across Devices
harmony 鸿蒙Cross-Device Notification Management (for System Applications Only)
harmony 鸿蒙Cross-Device Notification Overview
harmony 鸿蒙Introduction to Notification Kit
harmony 鸿蒙Enabling Quick Reply for Cross-device Notifications
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦