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 duplicate 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 device management list, set the notDistributed field in the NotificationRequest parameter to false and forceDistributed to true.
Available APIs
| API | Description | Description |
|---|---|---|
| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | Publishes a notification. | For details, see the description of the notDistributed and forceDistributed fields in the NotificationRequest object. |
Prerequisites
- The user has connected the watch to the phone through the Huawei Health app.
- 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.
How to Develop
-
Import the related modules.
import { notificationManager } from '@kit.NotificationKit'; import { BusinessError } from '@kit.BasicServicesKit'; -
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 the notification to be published only on the current device. notDistributed: true }; notificationManager.publish(notificationRequest, publishCallback); -
Publish the notification across devices based on the device 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' } }, // forceDistributed takes effect only when the application is in the device management list and the notDistributed field is set to false. If forceDistributed is set to false, the notification is published to the devices according to the device management list. notDistributed: false, forceDistributed: false }; notificationManager.publish(notificationRequest, publishCallback);