【鸿蒙HarmonyOS开发笔记】通知模块之为通知添加行为意图 |
您所在的位置:网站首页 › 为什么没有收到鸿蒙升级短信通知 › 【鸿蒙HarmonyOS开发笔记】通知模块之为通知添加行为意图 |
概述
WantAgent提供了封装行为意图的能力,这里所说的行为意图主要是指拉起指定的应用组件及发布公共事件等能力。HarmonyOS支持以通知的形式,将WantAgent从发布方传递至接收方,从而在接收方触发WantAgent中指定的意图。例如,在通知消息的发布者发布通知时,通常期望用户可以通过通知栏点击拉起目标应用组件。为了达成这一目标,开发者可以将WantAgent封装至通知消息中,当系统接收到WantAgent后,在用户点击通知栏时触发WantAgent的意图,从而拉起目标应用组件。 为通知添加行为意图的实现方式如下图所示:发布通知的应用向应用组件管理服务AMS(Ability Manager Service)申请WantAgent,然后随其他通知信息一起发送给桌面,当用户在桌面通知栏上点击通知时,触发WantAgent动作 携带行为意图的通知运行机制 接口说明 具体接口描述,详见WantAgent接口文档。 getWantAgent(info: WantAgentInfo, callback: AsyncCallback): void 创建WantAgent trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback): void 触发WantAgent意图 cancel(agent: WantAgent, callback: AsyncCallback): void 取消WantAgent getWant(agent: WantAgent, callback: AsyncCallback): void 获取WantAgent的want equal(agent: WantAgent, otherAgent: WantAgent, callback: AsyncCallback): void 判断两个WantAgent实例是否相等 开发步骤导入模块 import NotificationManager from '@ohos.notificationManager'; import wantAgent from '@ohos.app.ability.wantAgent';创建WantAgentInfo信息 创建拉起Ability的WantAgent的WantAgentInfo信息,详细文档请点击这里 let wantAgentObj = null; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。 // 通过WantAgentInfo的operationType设置动作类型。 let wantAgentInfo = { wants: [ { deviceId: '', bundleName: 'com.example.test', abilityName: 'com.example.test.MainAbility', action: '', entities: [], uri: '', parameters: {} } ], operationType: wantAgent.OperationType.START_ABILITY, requestCode: 0, wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG] }创建WantAgent // 创建WantAgent wantAgent.getWantAgent(wantAgentInfo, (err, data) => { if (err) { console.error('[WantAgent]getWantAgent err=' + JSON.stringify(err)); } else { console.info('[WantAgent]getWantAgent success'); wantAgentObj = data; } });构造NotificationRequest对象 // 构造NotificationRequest对象 let notificationRequest = { content: { contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: 'Test_Title', text: 'Test_Text', additionalText: 'Test_AdditionalText', }, }, id: 1, label: 'TEST', wantAgent: wantAgentObj, }发布WantAgent通知 // 通知发送 NotificationManager.publish(notificationRequest, (err) => { if (err) { console.error(`[ANS] failed to publish, error[${err}]`); return; } console.info(`[ANS] publish success `); });用户通过点击通知栏上的通知,即可触发WantAgent的动作 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |