1# 为通知添加行为意图 2 3应用向Ability Kit申请[WantAgent](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md),并将WantAgent封装至通知中。当发布通知时,用户便可以通过点击通知栏中的消息或按钮,拉起目标应用组件或发布公共事件。 4 5携带了actionButtons的通知示意图如下。 6 7 8 9## 运行机制 10 11 12 13## 接口说明 14 15| **接口名** | **描述** | 16| -------- | -------- | 17| [publish](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerpublish-1)(request: NotificationRequest): Promise\<void\> | 发布通知。 | 18| [getWantAgent](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md#wantagentgetwantagent)(info: WantAgentInfo, callback: AsyncCallback<WantAgent>): void | 创建WantAgent。 | 19 20## 开发步骤 21 221. 导入模块。 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. 创建WantAgentInfo信息。 35 36 场景一:创建拉起UIAbility的WantAgent的[WantAgentInfo](../reference/apis-ability-kit/js-apis-inner-wantAgent-wantAgentInfo.md)信息。 37 38 ```typescript 39 let wantAgentObj:WantAgent; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。 40 41 // 通过WantAgentInfo的operationType设置动作类型 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 场景二:创建发布[公共事件](../basic-services/common-event/common-event-overview.md)的WantAgent的[WantAgentInfo](../reference/apis-ability-kit/js-apis-inner-wantAgent-wantAgentInfo.md)信息。 61 62 ```typescript 63 let wantAgentObj:WantAgent; // 用于保存创建成功的WantAgent对象,后续使用其完成触发的动作。 64 65 // 通过WantAgentInfo的operationType设置动作类型 66 let wantAgentInfo:wantAgent.WantAgentInfo = { 67 wants: [ 68 { 69 action: 'event_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. 调用[getWantAgent()](../reference/apis-ability-kit/js-apis-app-ability-wantAgent.md#wantagentgetwantagent)方法进行创建WantAgent。 80 81 ```typescript 82 // 创建WantAgent 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. 构造NotificationRequest对象,并发布携带WantAgent的通知。 94 95 > **说明:** 96 > 97 > - 如果封装WantAgent至通知消息中,可以点击通知触发WantAgent。当通知消息存在actionButtons时,点击通知会先显示actionButtons,再次点击通知触发WantAgent。 98 > 99 > - 如果封装WantAgent至通知按钮中,点击通知后,该通知下方会出现通知按钮,可以点击按钮触发WantAgent。 100 101 ```typescript 102 // 构造NotificationActionButton对象 103 let actionButton: notificationManager.NotificationActionButton = { 104 title: 'Test_Title', 105 // wantAgentObj使用前需要保证已被赋值(即步骤3执行完成) 106 // 通知按钮的WantAgent 107 wantAgent: wantAgentObj 108 } 109 110 // 构造NotificationRequest对象 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 122 wantAgent: wantAgentObj, 123 // 通知按钮 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## 示例代码 138 139 - [自定义通知](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/BasicFeature/Notification/CustomNotification/README_zh.md) 140<!--RP1End-->