1# Adding a WantAgent Object to a Notification 2 3When publishing a notification, if you want users to tap the notification panel to start the target application component or publish a public event, you can use the Ability Kit to apply for [WantAgent](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md) and encapsulate it into the notification message. 4 5**Figure 1** Publishing a notification with a WantAgent object 6  7 8## Available APIs 9 10For details about the APIs, see [@ohos.wantAgent (WantAgent)](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md). 11 12| **API**| **Description**| 13| -------- | -------- | 14| getWantAgent(info: WantAgentInfo, callback: AsyncCallback<WantAgent>): void | Creates a **WantAgent** object.| 15 16## How to Develop 17 181. Import the modules. 19 20 ```typescript 21 import { notificationManager } from '@kit.NotificationKit'; 22 import { wantAgent, WantAgent } from '@kit.AbilityKit'; 23 import { BusinessError } from '@kit.BasicServicesKit'; 24 import { hilog } from '@kit.PerformanceAnalysisKit'; 25 26 const TAG: string = '[PublishOperation]'; 27 const DOMAIN_NUMBER: number = 0xFF00; 28 ``` 29 302. Create a **WantAgentInfo** object. 31 32 Scenario 1: Create a [WantAgentInfo](../reference/apis-ability-kit/js-apis-inner-wantAgent-wantAgentInfo.md) object for starting a UIAbility. 33 34 ```typescript 35 let wantAgentObj:WantAgent; // Save the created WantAgent object for completing the trigger operations at a later time. 36 37 // Set the action type through operationType of WantAgentInfo. 38 let wantAgentInfo:wantAgent.WantAgentInfo = { 39 wants: [ 40 { 41 deviceId: '', 42 bundleName: 'com.samples.notification', 43 abilityName: 'SecondAbility', 44 action: '', 45 entities: [], 46 uri: '', 47 parameters: {} 48 } 49 ], 50 actionType: wantAgent.OperationType.START_ABILITY, 51 requestCode: 0, 52 wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG] 53 }; 54 ``` 55 56 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). 57 58 ```typescript 59 let wantAgentObj:WantAgent; // Save the created WantAgent object for completing the trigger operations at a later time. 60 61 // Set the action type through operationType of WantAgentInfo. 62 let wantAgentInfo:wantAgent.WantAgentInfo = { 63 wants: [ 64 { 65 action: 'event_name', // Set the action name. 66 parameters: {}, 67 } 68 ], 69 actionType: wantAgent.OperationType.SEND_COMMON_EVENT, 70 requestCode: 0, 71 wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG], 72 }; 73 ``` 74 753. Call [getWantAgent()](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md#wantagentgetwantagent) to create a **WantAgent** object. 76 77 ```typescript 78 // Create a WantAgent object. 79 wantAgent.getWantAgent(wantAgentInfo, (err: BusinessError, data:WantAgent) => { 80 if (err) { 81 hilog.error(DOMAIN_NUMBER, TAG, `Failed to get want agent. Code is ${err.code}, message is ${err.message}`); 82 return; 83 } 84 hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in getting want agent.'); 85 wantAgentObj = data; 86 }); 87 ``` 88 894. Create a **NotificationRequest** object and publish a notification that carries the **WantAgent** object. 90 91 ```typescript 92 // Create a NotificationRequest object. 93 let notificationRequest: notificationManager.NotificationRequest = { 94 content: { 95 notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 96 normal: { 97 title: 'Test_Title', 98 text: 'Test_Text', 99 additionalText: 'Test_AdditionalText', 100 }, 101 }, 102 id: 6, 103 label: 'TEST', 104 // Before using wantAgentObj, ensure that a value has been assigned to it (that is, step 3 is performed). 105 wantAgent: wantAgentObj, 106 } 107 108 notificationManager.publish(notificationRequest, (err: BusinessError) => { 109 if (err) { 110 hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish notification. Code is ${err.code}, message is ${err.message}`); 111 return; 112 } 113 hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in publishing notification.'); 114 }); 115 ``` 116 1175. When the user touches the notification from the notification panel, the system automatically triggers the action specified in the **WantAgent** object. 118