为通知添加行为意图

WantAgent提供了封装行为意图的能力,该行为意图是指拉起指定的应用组件及发布公共事件等能力。支持以通知的形式,将WantAgent从发布方传递至接收方,从而在接收方触发WantAgent中指定的意图。例如在通知消息的发布者发布通知时,通常期望用户可以通过通知栏点击拉起目标应用组件。为了达成这一目标,开发者可以将WantAgent封装至通知消息中,当系统接收到WantAgent后,在用户点击通知栏时触发WantAgent的意图,从而拉起目标应用组件。

为通知添加行为意图的实现方式如下图所示:发布通知的应用向应用组件管理服务AMS(Ability Manager Service)申请WantAgent,然后随其他通知信息一起发送给桌面,当用户在桌面通知栏上点击通知时,触发WantAgent动作。

图1 携带行为意图的通知运行机制
notification-with-wantagent

接口说明

具体接口描述,详见WantAgent接口文档

接口名 描述
getWantAgent(info: WantAgentInfo, callback: AsyncCallback<WantAgent>): void 创建WantAgent。
trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback<CompleteData>): void 触发WantAgent意图。
cancel(agent: WantAgent, callback: AsyncCallback<void>): void 取消WantAgent。
getWant(agent: WantAgent, callback: AsyncCallback<Want>): void 获取WantAgent的want。
equal(agent: WantAgent, otherAgent: WantAgent, callback: AsyncCallback<boolean>): void 判断两个WantAgent实例是否相等。

开发步骤

  1. 请求通知授权,获得用户授权后,才能使用通知功能。

  2. 导入模块。

    import notificationManager from '@ohos.notificationManager';
    import wantAgent from '@ohos.app.ability.wantAgent';
    import { WantAgent } from '@ohos.app.ability.wantAgent';
    import Base from '@ohos.base';
    
  3. 创建WantAgentInfo信息。

    场景一:创建拉起UIAbility的WantAgent的WantAgentInfo信息。

    let wantAgentObj:WantAgent; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。
    
    // 通过WantAgentInfo的operationType设置动作类型
    let wantAgentInfo:wantAgent.WantAgentInfo = {
      wants: [
        {
          deviceId: '',
          bundleName: 'com.samples.notification',
          abilityName: 'SecondAbility',
          action: '',
          entities: [],
          uri: '',
          parameters: {}
        }
      ],
      operationType: wantAgent.OperationType.START_ABILITY,
      requestCode: 0,
      wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
    };
    

    场景二:创建发布公共事件的WantAgent的WantAgentInfo信息。

    let wantAgentObj:WantAgent; // 用于保存创建成功的WantAgent对象,后续使用其完成触发的动作。
    
    // 通过WantAgentInfo的operationType设置动作类型
    let wantAgentInfo:wantAgent.WantAgentInfo = {
      wants: [
        {
          action: 'event_name', // 设置事件名
          parameters: {},
        }
      ],
      operationType: wantAgent.OperationType.SEND_COMMON_EVENT,
      requestCode: 0,
      wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG],
    };
    
  4. 调用getWantAgent()方法进行创建WantAgent。

    // 创建WantAgent
    wantAgent.getWantAgent(wantAgentInfo, (err:Base.BusinessError, data:WantAgent) => {
      if (err) {
        console.error(`Failed to get want agent. Code is ${err.code}, message is ${err.message}`);
        return;
      }
      console.info('Succeeded in getting want agent.');
      wantAgentObj = data;
    });
    
  5. 构造NotificationRequest对象,并发布WantAgent通知。

    // 构造NotificationRequest对象
    let notificationRequest: notificationManager.NotificationRequest = {
      content: {
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: 'Test_Title',
          text: 'Test_Text',
          additionalText: 'Test_AdditionalText',
        },
      },
      id: 6,
      label: 'TEST',
      wantAgent: wantAgentObj,
    }
    
    notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
      if (err) {
        console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
        return;
      }
      console.info('Succeeded in publishing notification.');
    });
    
  6. 用户通过点击通知栏上的通知,系统会自动触发WantAgent的动作。