harmony 鸿蒙Canceling a Notification

  • 2025-06-12
  • 浏览 (14)

Canceling a Notification

When a user taps a notification and starts the application in the foreground, the application can cancel one or all notifications.

For example, after a user receives an IM from a friend and taps a notification to view the message, the application can cancel the notification.

Available APIs

The table below lists some APIs for canceling notifications. For details about the APIs, see @ohos.notificationManager (NotificationManager).

API Description
cancel(id: number, callback: AsyncCallback<void>): void Cancels a notification.
cancelAll(callback: AsyncCallback<void>): void Cancels all notifications published by the application.

How to Develop

The following describes how to cancel a text notification. This procedure is applicable for canceling other types of notifications.

  1. Import modules.
   import { notificationManager } from '@kit.NotificationKit';
   import { BusinessError } from '@kit.BasicServicesKit';
   import { hilog } from '@kit.PerformanceAnalysisKit';
   
   const TAG: string = '[PublishOperation]';
   const DOMAIN_NUMBER: number = 0xFF00;
  1. Publish a notification.

For details, see Publishing a Text Notification.

  1. Cancel the notification.
    // After the application is started in the foreground and the message is viewed, call this API to cancel the notification.
    notificationManager.cancel(1, (err: BusinessError) => {
      if (err) {
        hilog.error(DOMAIN_NUMBER, TAG, `Failed to cancel notification. Code is ${err.code}, message is ${err.message}`);
        return;
      }
      hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in canceling notification.');
    });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Notification Kit (User Notification Service)

harmony 鸿蒙Publishing a Live View Notification (for System Applications Only)

harmony 鸿蒙Managing the Notification Badge

harmony 鸿蒙Clearing Repeated Notifications Across Devices

harmony 鸿蒙Cross-Device Notification Management (for System Applications Only)

harmony 鸿蒙Cross-Device Notification Overview

harmony 鸿蒙Requesting Notification Authorization

harmony 鸿蒙Introduction to Notification Kit

harmony 鸿蒙Enabling Quick Reply for Cross-device Notifications

harmony 鸿蒙Managing Notification Slots

0  赞