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

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

Cross-Device Notification Management (for System Applications Only)

By default, notifications are published across devices. If an application has implemented notification across devices (for example, SMS notifications are sent to devices such as watches, tablets, and 2-in-1 devices), you need to manage the distributed devices to avoid repeated notifications.

Since API version 18, a system application is supported to publish notifications in the following manners:

  • If a notification of an application is published only on the current device, set the notDistributed field in the NotificationRequest parameter to true.
  • If a notification of an application is published based on the cross-device collaborative management list, set the notDistributed field in the NotificationRequest parameter to false and forceDistributed to true.

Available APIs

API Description Note
publish(request: NotificationRequest): Promise<void> Publishes a notification. For details, see the description of the notDistributed and forceDistributed fields in the NotificationRequest object.

How to Develop

  1. Import the related modules.

    import { notificationManager } from '@kit.NotificationKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
  2. Set the manner for publishing a notification.

    • Publish the notification only on the current device.
    // publish callback
    let publishCallback = (err: BusinessError): void => {
      if (err) {
        console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
      } else {
        console.info(`Succeeded in publishing notification.`);
      }
    }
    // NotificationRequest object
    let notificationRequest: notificationManager.NotificationRequest = {
      id: 1,
      content: {
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: "test_title",
          text: "test_text",
          additionalText: "test_additionalText"
          }
        },
        // Set notDistributed to true, indicating that the notification is not published across devices.
        notDistributed: true
    };
    notificationManager.publish(notificationRequest, publishCallback);
    
    • Publish the notification based on the cross-device collaborative management list.
    // publish callback
    let publishCallback = (err: BusinessError): void => {
      if (err) {
        console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
      } else {
        console.info(`Succeeded in publishing notification.`);
      }
    }
    // NotificationRequest object
    let notificationRequest: notificationManager.NotificationRequest = {
      id: 1,
      content: {
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: "test_title",
          text: "test_text",
          additionalText: "test_additionalText"
          }
        },
        // Set notDistributed to false and forceDistributed to true, indicating that the notification is published across devices based on the cross-device collaborative management list.
        notDistributed: false,
        forceDistributed: true
    };
    notificationManager.publish(notificationRequest, publishCallback);
    

你可能感兴趣的鸿蒙文章

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 Overview

harmony 鸿蒙Requesting Notification Authorization

harmony 鸿蒙Introduction to Notification Kit

harmony 鸿蒙Enabling Quick Reply for Cross-device Notifications

harmony 鸿蒙Managing Notification Slots

0  赞