1# Cross-Device Notification Management (for System Applications Only) 2 3<!--Kit: Notification Kit--> 4<!--Subsystem: Notification--> 5<!--Owner: @peixu--> 6<!--Designer: @dongqingran; @wulong158--> 7<!--Tester: @wanghong1997--> 8<!--Adviser: @huipeizi--> 9 10By 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 duplicate notifications. 11 12Since API version 18, a system application is supported to publish notifications in the following manners: 13 14- If a notification of an application is published only on the current device, set the **notDistributed** field in the [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest-sys.md) parameter to **true**. 15- If a notification of an application is published based on the device management list, set the **notDistributed** field in the [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest-sys.md) parameter to **false** and **forceDistributed** to **true**. 16 17## Available APIs 18 19| **API** | **Description**| **Note**| 20| -------- | -------- |-------- | 21| [publish](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerpublish-1)(request: NotificationRequest): Promise\<void\> | Publishes a notification. | For details, see the description of the **notDistributed** and **forceDistributed** fields in the [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest-sys.md) object.| 22| [publish](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerpublish)(request: NotificationRequest, callback: AsyncCallback\<void\>): void | Publishes a notification.| For details, see the description of the **notDistributed** and **forceDistributed** fields in the [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest-sys.md) object.| 23 24## Prerequisites 25 26 - The user has connected the watch to the phone through the Huawei Health app. 27 - The user has turned on the switch for syncing notifications from phone to watch for specified applications in **Huawei Health** > **Devices** > **Notifications** on their phones. 28 29## How to Develop 30 311. Import the related modules. 32 33 ```typescript 34 import { notificationManager } from '@kit.NotificationKit'; 35 import { BusinessError } from '@kit.BasicServicesKit'; 36 ``` 37 382. Set the manner for publishing a notification. 39 40 - Publish the notification only on the current device. 41 42 ```typescript 43 // publish callback 44 let publishCallback = (err: BusinessError): void => { 45 if (err) { 46 console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`); 47 } else { 48 console.info(`Succeeded in publishing notification.`); 49 } 50 } 51 // NotificationRequest object 52 let notificationRequest: notificationManager.NotificationRequest = { 53 id: 1, 54 content: { 55 notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 56 normal: { 57 title: 'test_title', 58 text: 'test_text', 59 additionalText: 'test_additionalText' 60 } 61 }, 62 // Set the notification to be published only on the current device. 63 notDistributed: true 64 }; 65 notificationManager.publish(notificationRequest, publishCallback); 66 ``` 67 68 - Publish the notification across devices based on the device management list. 69 70 ```typescript 71 // publish callback 72 let publishCallback = (err: BusinessError): void => { 73 if (err) { 74 console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`); 75 } else { 76 console.info(`Succeeded in publishing notification.`); 77 } 78 } 79 // NotificationRequest object 80 let notificationRequest: notificationManager.NotificationRequest = { 81 id: 1, 82 content: { 83 notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 84 normal: { 85 title: 'test_title', 86 text: 'test_text', 87 additionalText: 'test_additionalText' 88 } 89 }, 90 // If forceDistributed is set to true, it takes effect only when the application is in the device management list and the notDistributed field is not set. If forceDistributed is set to false, the notification is published to the devices according to the device management list. 91 notDistributed: false, 92 forceDistributed: true 93 }; 94 notificationManager.publish(notificationRequest, publishCallback); 95 ``` 96