1# 为通知添加行为意图 2 3当发布通知时,如果期望用户可以通过点击通知栏拉起目标应用组件或发布公共事件,可以通过Ability Kit申请[WantAgent](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md)封装至通知消息中。 4 5**图1** 携带行为意图的通知运行机制 6  7 8## 接口说明 9 10具体接口描述,详见[WantAgent接口文档](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md)。 11 12| **接口名** | **描述** | 13| -------- | -------- | 14| getWantAgent(info: WantAgentInfo, callback: AsyncCallback<WantAgent>): void | 创建WantAgent。 | 15 16## 开发步骤 17 181. 导入模块。 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. 创建WantAgentInfo信息。 31 32 场景一:创建拉起UIAbility的WantAgent的[WantAgentInfo](../reference/apis-ability-kit/js-apis-inner-wantAgent-wantAgentInfo.md)信息。 33 34 ```typescript 35 let wantAgentObj:WantAgent; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。 36 37 // 通过WantAgentInfo的operationType设置动作类型 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 场景二:创建发布[公共事件](../basic-services/common-event/common-event-overview.md)的WantAgent的[WantAgentInfo](../reference/apis-ability-kit/js-apis-inner-wantAgent-wantAgentInfo.md)信息。 57 58 ```typescript 59 let wantAgentObj:WantAgent; // 用于保存创建成功的WantAgent对象,后续使用其完成触发的动作。 60 61 // 通过WantAgentInfo的operationType设置动作类型 62 let wantAgentInfo:wantAgent.WantAgentInfo = { 63 wants: [ 64 { 65 action: 'event_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. 调用[getWantAgent()](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md#wantagentgetwantagent)方法进行创建WantAgent。 76 77 ```typescript 78 // 创建WantAgent 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. 构造NotificationRequest对象,并发布WantAgent通知。 90 91 ```typescript 92 // 构造NotificationRequest对象 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 // wantAgentObj使用前需要保证已被赋值(即步骤3执行完成) 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. 用户通过点击通知栏上的通知,系统会自动触发WantAgent的动作。 118