【中秋国庆不断更】HarmonyOS对通知类消息的管理与发布通知(下) ...

打印 上一主题 下一主题

主题 864|帖子 864|积分 2602

【中秋国庆不断更】HarmonyOS对通知类消息的管理与发布通知(下)

一、发布进度条类型通知

进度条通知也是常见的通知类型,主要应用于文件下载、事务处理进度显示。HarmonyOS提供了进度条模板,发布通知应用设置好进度条模板的属性值,如模板名、模板数据,通过通知子系统发送到通知栏显示。
目前系统模板仅支持进度条模板,通知模板NotificationTemplate中的data参数为用户自定义数据,用于显示与模块相关的数据,效果示意如下图所示。

接口说明

isSupportTemplate()是查询模板是否支持接口,目前仅支持进度条模板。
接口名描述isSupportTemplate(templateName: string, callback: AsyncCallback): void查询模板是否存在。开发步骤

​            1.     导入模块。
  1. import NotificationManager from '@ohos.notificationManager';
复制代码
​            2.     系统是否支持进度条模板,查询结果为支持downloadTemplate模板类通知。
  1. NotificationManager.isSupportTemplate('downloadTemplate').then((data) => {
  2.   console.info(`[ANS] isSupportTemplate success`);
  3.   let isSupportTpl: boolean = data; // isSupportTpl的值为true表示支持支持downloadTemplate模板类通知,false表示不支持
  4.   // ...
  5. }).catch((err) => {
  6.   console.error(`[ANS] isSupportTemplate failed, error[${err}]`);
  7. });
复制代码
说明
查询系统支持进度条模板后,再进行后续的步骤操作。
​            3.     构造进度条模板对象,并发布通知。
  1. let template = {
  2.   name:'downloadTemplate',
  3.   data: {
  4.     title: '标题:',
  5.     fileName: 'music.mp4',
  6.     progressValue: 30,
  7.     progressMaxValue:100,
  8.   }
  9. }
  10. //构造NotificationRequest对象
  11. let notificationRquest = {
  12.   id: 1,
  13.   slotType: notify.SlotType.OTHER_TYPES,
  14.   template: template,
  15.   content: {
  16.     contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
  17.     normal: {
  18.       title: template.data.title + template.data.fileName,
  19.       text: "sendTemplate",
  20.       additionalText: "30%"
  21.     }
  22.   },
  23.   deliveryTime: new Date().getTime(),
  24.   showDeliveryTime: true
  25. }
  26. notify.publish(notificationRquest).then(() => {
  27.   console.info(`[ANS] publish success `);
  28. }).catch((err) => {
  29.   console.error(`[ANS] failed to publish, error[${err}]`);
  30. });
复制代码
二、为通知添加行为意图

WantAgent提供了封装行为意图的能力,这里所说的行为意图主要是指拉起指定的应用组件及发布公共事件等能力。HarmonyOS支持以通知的形式,将WantAgent从发布方传递至接收方,从而在接收方触发WantAgent中指定的意图。例如,在通知消息的发布者发布通知时,通常期望用户可以通过通知栏点击拉起目标应用组件。为了达成这一目标,开发者可以将WantAgent封装至通知消息中,当系统接收到WantAgent后,在用户点击通知栏时触发WantAgent的意图,从而拉起目标应用组件。
为通知添加行为意图的实现方式如下图所示:发布通知的应用向应用组件管理服务AMS(Ability Manager Service)申请WantAgent,然后随其他通知信息一起发送给桌面,当用户在桌面通知栏上点击通知时,触发WantAgent动作。
图1 携带行为意图的通知运行机制

接口说明

具体接口描述,详见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实例是否相等。开发步骤

​            1.     导入模块。
  1. import NotificationManager from '@ohos.notificationManager';
  2. import wantAgent from '@ohos.app.ability.wantAgent';
复制代码
2.创建WantAgentInfo信息。
场景一:创建拉起Ability的WantAgent的WantAgentInfo信息。
  1. let wantAgentObj = null; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。
  2. // 通过WantAgentInfo的operationType设置动作类型。
  3. let wantAgentInfo = {
  4.     wants: [
  5.         {
  6.             deviceId: '',
  7.             bundleName: 'com.example.test',
  8.             abilityName: 'com.example.test.MainAbility',
  9.             action: '',
  10.             entities: [],
  11.             uri: '',
  12.             parameters: {}
  13.         }
  14.     ],
  15.     operationType: wantAgent.OperationType.START_ABILITY,
  16.     requestCode: 0,
  17.     wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
  18. }
复制代码
场景二:创建发布公共事件的WantAgent的WantAgentInfo信息。
  1. let wantAgentObj = null; // 用于保存创建成功的WantAgent对象,后续使用其完成触发的动作。
  2. // wantAgentInfo
  3. let wantAgentInfo = {
  4.     wants: [
  5.         {
  6.             action: 'event_name', // 设置事件名。
  7.             parameters: {},
  8.         }
  9.     ],
  10.     operationType: wantAgent.OperationType.SEND_COMMON_EVENT,
  11.     requestCode: 0,
  12.     wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG],
  13. }
复制代码
​            3.     创建WantAgent。
  1. // 创建WantAgent
  2. wantAgent.getWantAgent(wantAgentInfo, (err, data) => {
  3.     if (err) {
  4.         console.error('[WantAgent]getWantAgent err=' + JSON.stringify(err));
  5.     } else {
  6.         console.info('[WantAgent]getWantAgent success');
  7.         wantAgentObj = data;
  8.     }
  9. });
复制代码
​            4.     构造NotificationRequest对象。
  1. // 构造NotificationRequest对象
  2. let notificationRequest = {
  3.     content: {
  4.         contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
  5.         normal: {
  6.             title: 'Test_Title',
  7.             text: 'Test_Text',
  8.             additionalText: 'Test_AdditionalText',
  9.         },
  10.     },
  11.     id: 1,
  12.     label: 'TEST',
  13.     wantAgent: wantAgentObj,
  14. }
复制代码
​            5.     发布WantAgent通知。
  1. // 通知发送
  2. NotificationManager.publish(notificationRequest, (err) => {
  3.     if (err) {
  4.         console.error(`[ANS] failed to publish, error[${err}]`);
  5.         return;
  6.     }
  7.     console.info(`[ANS] publish success `);
  8. });
复制代码
​            6.     用户通过点击通知栏上的通知,即可触发WantAgent的动作。
本文由博客一文多发平台 OpenWrite 发布!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

来自云龙湖轮廓分明的月亮

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表