Implementing Custom Notifications in HarmonyOS

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

  1. Upon launching the application, a permission dialog will appear requesting notification access; tap 'Allow' to proceed.

  2. Click buttons on the interface to publish different notification types. Pull down the status bar to view them in the notification panel.

  3. When sound and vibration toggles are enabled, clicking corresponding buttons will trigger both notification and associated audio/vibration effects.

  4. 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.

  5. Return to the home screen to observe the badge number reflecting the unread message count (requires an installed launcher app).

  6. 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 entering Index.ets, which invokes notification.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 through getAllNotifications().

  • Notification type-specific cancellation is achieved via cancelNotificationType().

  • Badge handling includes setBadgeNumber() and getBadgeNumber() methods.

  • Adding a new notification is done via addNotification().

  • NotificationOperations exposes public methods for interaction with UI components.

  • On the Index.ets page, tapping buttons triggers respective methods in NotificationOperations. Content is retrieved from NotificationContentUtil, assembled into a full request using NotificationRequestUtil, and finally published via NotificationUtil.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 NotificationUtil that 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 via startAbility(), followed by locating components using assertComponentExist, findComponent, and findWindow, then simulates clicks using click().

  • Each it() block in Index.test.ets defines a sequence of componant interactions to simulate real user behavior, collectively forming comprehensive automated test coverage.

Required Permissions

  1. For vibration functionality, the permission ohos.permission.VIBRATE is required. This normal-level permission allows apps to control motor vibrations and is granted at the system level.

Dependencies

  1. The DaYu 200 board does not come with a built-in vibrator; one must be manual installed.

  2. Badge display depends on a third-party launcher application.

Constraints and Limitations

  1. This sample supports standard system environments and is compatible with RK3568 devices.

  2. Manual signing of the application is necessary.

  3. The project uses Stage model and targets API version 10 SDK.

  4. 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

Tags: HarmonyOS notification customization ArkTS mobile-development

Posted on Sat, 06 Jun 2026 17:39:35 +0000 by forcer