• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Adding a WantAgent Object to a Notification
2
3An application requests [WantAgent](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md) from Ability Kit and encapsulates it into the notification. When a notification is published, the user may tap a message or a button in the notification panel to start the target application or publish a common event.
4
5The following figure shows a notification carrying action buttons.
6
7![notification_wantagent](figures/notification_actionButtons.png)
8
9## Working Principles
10
11![notification_wantagent](figures/notification_wantagent.png)
12
13## Available APIs
14
15| **API**| **Description**|
16| -------- | -------- |
17| [publish](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerpublish-1)(request: NotificationRequest): Promise\<void\>       | Publishes a notification. |
18| [getWantAgent](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md#wantagentgetwantagent)(info:&nbsp;WantAgentInfo,&nbsp;callback:&nbsp;AsyncCallback&lt;WantAgent&gt;):&nbsp;void | Creates a **WantAgent** object.|
19
20## How to Develop
21
221. Import the modules.
23
24   ```typescript
25   import { notificationManager } from '@kit.NotificationKit';
26   import { wantAgent, WantAgent } from '@kit.AbilityKit';
27   import { BusinessError } from '@kit.BasicServicesKit';
28   import { hilog } from '@kit.PerformanceAnalysisKit';
29
30   const TAG: string = '[PublishOperation]';
31   const DOMAIN_NUMBER: number = 0xFF00;
32   ```
33
342. Create a **WantAgentInfo** object.
35
36   Scenario 1: Create a [WantAgentInfo](../reference/apis-ability-kit/js-apis-inner-wantAgent-wantAgentInfo.md) object for starting a UIAbility.
37
38   ```typescript
39   let wantAgentObj:WantAgent; // Save the created WantAgent object for completing the trigger operations at a later time.
40
41   // Set the action type through operationType of WantAgentInfo.
42   let wantAgentInfo:wantAgent.WantAgentInfo = {
43     wants: [
44       {
45         deviceId: '',
46         bundleName: 'com.samples.notification',
47         abilityName: 'SecondAbility',
48         action: '',
49         entities: [],
50         uri: '',
51         parameters: {}
52       }
53     ],
54     actionType: wantAgent.OperationType.START_ABILITY,
55     requestCode: 0,
56     actionFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
57   };
58   ```
59
60   Scenario 2: Create a [WantAgentInfo](../reference/apis-ability-kit/js-apis-inner-wantAgent-wantAgentInfo.md) object for publishing a [common event](../basic-services/common-event/common-event-overview.md).
61
62   ```typescript
63   let wantAgentObj:WantAgent; // Save the created WantAgent object for completing the trigger operations at a later time.
64
65   // Set the action type through operationType of WantAgentInfo.
66   let wantAgentInfo:wantAgent.WantAgentInfo = {
67     wants: [
68       {
69         action: 'event_name', // Set the action name.
70         parameters: {},
71       }
72     ],
73     actionType: wantAgent.OperationType.SEND_COMMON_EVENT,
74     requestCode: 0,
75     actionFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG],
76   };
77   ```
78
793. Call [getWantAgent()](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md#wantagentgetwantagent) to create a **WantAgent** object.
80
81   ```typescript
82   // Create a WantAgent object.
83   wantAgent.getWantAgent(wantAgentInfo, (err: BusinessError, data:WantAgent) => {
84     if (err) {
85       hilog.error(DOMAIN_NUMBER, TAG, `Failed to get want agent. Code is ${err.code}, message is ${err.message}`);
86       return;
87     }
88     hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in getting want agent.');
89     wantAgentObj = data;
90   });
91   ```
92
934. Create a **NotificationRequest** object and publish a notification carrying **WantAgent**.
94
95   > **NOTE**
96   >
97   > - If **WantAgent** is encapsulated in a notification, **WantAgent** is triggered when the notification is tapped. If a notification contains action buttons, the buttons are displayed when the notification is tapped and **WantAgent** is triggered when the notification is tapped again.
98   >
99   > - If **WantAgent** is encapsulated in the action buttons, the buttons are displayed under the notification when the notification is tapped and **WantAgent** is triggered when a button is tapped.
100
101   ```typescript
102   // Create the NotificationActionButton object.
103   let actionButton: notificationManager.NotificationActionButton = {
104     title: 'Test_Title',
105     // Before using wantAgentObj, ensure that a value has been assigned to it (that is, step 3 is performed).
106     // WantAgent of the notification buttons
107     wantAgent: wantAgentObj
108   }
109
110   // Create a NotificationRequest object.
111   let notificationRequest: notificationManager.NotificationRequest = {
112     content: {
113       notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
114       normal: {
115         title: 'Test_Title',
116         text: 'Test_Text',
117         additionalText: 'Test_AdditionalText',
118       },
119     },
120     id: 6,
121     // WantAgent of the notification
122     wantAgent: wantAgentObj,
123     // Action buttons
124     actionButtons: [actionButton],
125   }
126
127   notificationManager.publish(notificationRequest, (err: BusinessError) => {
128     if (err) {
129       hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
130       return;
131     }
132     hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in publishing notification.');
133   });
134   ```
135<!--RP1-->
136
137## Sample Code
138
139  - [Custom Notification](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/BasicFeature/Notification/CustomNotification/README.md)
140<!--RP1End-->
141