• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Clearing Repeated Notifications Across Devices
2
3<!--Kit: Notification Kit-->
4<!--Subsystem: Notification-->
5<!--Owner: @peixu-->
6<!--Designer: @dongqingran; @wulong158-->
7<!--Tester: @wanghong1997-->
8<!--Adviser: @huipeizi-->
9
10Since API version 20, you can clear repeated notifications across devices to prevent users from being disturbed by notifications published through different ways (for example, notifications sent from a mobile phone to the current device are the same as those published by Push Kit).
11
12## Implementation Principles
13
14When an application publishes a notification, the notification carries the [appMessageId](../../application-dev/reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest-1) field, which is a unique identifier. After receiving notifications from multiple ways, the cross-device notification service checks the notification based on this field to clear duplicate notifications.
15
16Only the first notification is displayed on the device. Duplicate notifications received later will be silently cleared and will not be displayed or notified.
17
18**Figure 1** Process of notification deduplication in all scenarios
19
20![distributed_messageid](figures/notification_messageid.png)
21
22## Constraints
23
24- The uniqueness of the [appMessageId](../../application-dev/reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest-1) field must be ensured. This field for the same notification must stay consistent on all devices.
25- The [appMessageId](../../application-dev/reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest-1) field is valid only within 24 hours after the notification is published. If the notification is published beyond 24 hours or the device is restarted, this field becomes invalid.
26
27## Available APIs
28
29| **API** | **Description**|**Description**|
30| -------- | -------- | -------- |
31| [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 **appMessageId** field in the [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md) object.|
32
33## How to Develop
34
351. Import the related modules.
36
37    ```typescript
38    import { notificationManager } from '@kit.NotificationKit';
39    import { BusinessError } from '@kit.BasicServicesKit';
40    ```
41
422. Publish a notification that contains the **appMessageId** field.
43
44    ```typescript
45    // publish callback
46    let publishCallback = (err: BusinessError): void => {
47        if (err) {
48            console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
49        } else {
50            console.info(`Succeeded in publishing notification.`);
51        }
52    }
53    // NotificationRequest object
54    let notificationRequest: notificationManager.NotificationRequest = {
55        id: 1,
56        content: {
57            notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
58            normal: {
59            title: 'test_title',
60            text: 'test_text',
61            additionalText: 'test_additionalText'
62            }
63        },
64        appMessageId: 'test_appMessageId_1'
65    };
66    notificationManager.publish(notificationRequest, publishCallback);
67    ```
68