Overview
This example demonstrates how to create and manage various types of notifications using @ohos.notificationManager and related APIs. It covers publishing, canceling, and setting badge counts on the desktop for different notification formats including basic, long text, multi-line, image-based, button-enabled, and app-launching notifications.
Preview
Usage Instructions
-
Upon launching the application, a permission dialog will appear requesting notification access; tap 'Allow' to proceed.
-
Click buttons on the interface to publish different notification types. Pull down the status bar to view them in the notification panel.
-
When sound and vibration toggles are enabled, clicking corresponding buttons will trigger both notification and associated audio/vibration effects.
-
Switch to the Messages tab to see previously sent notifications. Each message displays a count indicator. Tap on messages to mark them as read or dismiss specific notifications.
-
Return to the home screen to observe the badge number reflecting the unread message count (requires an installed launcher app).
-
Use the "Clear All" option to remove all notifications published by this application.
Project Structure
entry/src/main/ets/
|---Application
|---components
| |---NotificationList.ets // Notification list component
| |---NotificationPublish.ets // Notification publishing component
| |---TitleBar.ets // Title bar component
|---feature
| |---NotificationOperations.ets // Public interfaces for notification publishing
|---MainAbility
|---pages
| |---Index.ets // Main page
entry/src/ohosTest/ets/
|---test
| |---Index.test.ets // Automated tests for main page
notification/src/main/ets/
|---notification
| |---NotificationContentUtil.ets // Utility for constructing notification content
| |---NotificationManagementUtil.ets // Utility for managing message lists and badges
| |---NotificationRequestUtil.ets // Utility for building complete notification requests
| |---NotificationUtil.ets // Utility for enabling, publishing, and dismissing notifications
| |---WantAgentUtil.ets // Utility for handling want agents
|---util // Logging utilities
Implementation Details
-
Notification enablement, publication, and cancellation are encapsulated within
NotificationUtil. -
To request notification permission, call
notificationUtil.enableNotification()before enteringIndex.ets, which invokesnotification.requestEnableNotification(). -
Publishing is handled via
publishNotification()method. -
Cancellation can be performed either globally (
cancelAllNotifications()) or by ID (cancelNotificationById()). -
Message retrieval and badge management functionalities are implemented in
NotificationManagementUtil. -
Retrieval uses
getActiveNotifications()from @ohos.notificationManager to fetch active notifications and their counts throughgetAllNotifications(). -
Notification type-specific cancellation is achieved via
cancelNotificationType(). -
Badge handling includes
setBadgeNumber()andgetBadgeNumber()methods. -
Adding a new notification is done via
addNotification(). -
NotificationOperationsexposes public methods for interaction with UI components. -
On the
Index.etspage, tapping buttons triggers respective methods inNotificationOperations. Content is retrieved fromNotificationContentUtil, assembled into a full request usingNotificationRequestUtil, and finally published viaNotificationUtil.publishNotification(). -
Audio and vibration settings are checked during notification publishing in
NotificationUtil. If enabled, appropriate action are executed. -
Sound and vibration toggle states are managed by calling setter methods in
NotificationUtilthat update internal flags accordingly. -
Automated testing includes unit tests for application and system APIs, as well as UI automation.
-
In
Index.test.ets,beforeAll()initializes the app viastartAbility(), followed by locating components usingassertComponentExist,findComponent, andfindWindow, then simulates clicks usingclick(). -
Each
it()block inIndex.test.etsdefines a sequence of componant interactions to simulate real user behavior, collectively forming comprehensive automated test coverage.
Required Permissions
- For vibration functionality, the permission
ohos.permission.VIBRATEis required. This normal-level permission allows apps to control motor vibrations and is granted at the system level.
Dependencies
-
The DaYu 200 board does not come with a built-in vibrator; one must be manual installed.
-
Badge display depends on a third-party launcher application.
Constraints and Limitations
-
This sample supports standard system environments and is compatible with RK3568 devices.
-
Manual signing of the application is necessary.
-
The project uses Stage model and targets API version 10 SDK.
-
DevEco Studio version 4.0 Release or higher is required for compilation and execution.
Download Instructions
To download this project, run:
git init
git config core.sparsecheckout trued
echo code/BasicFeature/Notification/CustomNotification/ > .git/info/sparse-checkout
git remote add origin https://gitee.com/openharmony/applications_app_samples.git
git pull origin master