HarmonyOS 通知消息点击隐式匹配打开ability失败?

通知消息点击隐式匹配打开ability失败。

根据文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/explicit-implicit-want-mappings-V5\#want%E5%8F%82%E6%95%B0%E7%9A%84action%E5%8C%B9%E9%85%8D%E8%A7%84%E5%88%99

传入abilityName必须为空,action不为空也可以拉起。

module.json5中定义:

{
 "name": "TestAbilityA",
 "srcEntry": "./ets/testabilityA/TestAbilityA.ets",
 "description": "$string:TestAbility1_desc",
 "icon": "$media:icon",
 "label": "$string:TestAbility1_label",
 "startWindowIcon": "$media:startIcon",
 "startWindowBackground": "$color:start_window_background",
 "skills": [
   {
     "entities": [
     "entity.open.page.a"
     ],
     "actions": [
     "action.open.page.a"
     ],
     "uris": [
     {
       "scheme": "https",
       "host": "xxx",
     },
     {
       "scheme": "http",
     }
     ]
   }
 ]
}

通知方法:

{
  "name": "TestAbilityA",
"srcEntry": "./ets/testabilityA/TestAbilityA.ets",
"description": "$string:TestAbility1_desc",
"icon": "$media:icon",
"label": "$string:TestAbility1_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"skills": [
  {
    "entities": [
    "entity.open.page.a"
    ],
    "actions": [
    "action.open.page.a"
    ],
    "uris": [
    {
      "scheme": "https",
    "host": "xxx",
    },
    {
      "scheme": "http",
    }
    ]
  }
  ]
}const TAG: string = '[PublishOperation]';
const DOMAIN_NUMBER: number = 0xFF00;
let wantAgentObj:WantAgent; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。
// 通过WantAgentInfo的operationType设置动作类型
let wantAgentInfo:wantAgent.WantAgentInfo = {
  wants: [
    {
      deviceId: '',
      bundleName: 'cxxx',
      abilityName: "",
      action: 'entity.open.page.a',
      entities: [],
      uri: '',
      parameters: {}
    }
  ],
  operationType: wantAgent.OperationType.START_ABILITY,
  requestCode: 0,
  wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
};

// 创建WantAgent
wantAgent.getWantAgent(wantAgentInfo, (err: BusinessError, data:WantAgent) => {
  if (err) {
    hilog.error(DOMAIN_NUMBER, TAG, `Failed to get want agent. Code is ${err.code}, message is ${err.message}`);
    return;
  }
  hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in getting want agent.');
  wantAgentObj = data;
  let notificationRequest: notificationManager.NotificationRequest = {
    id: 1,
    wantAgent:wantAgentObj,
    content: {
      notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知
      normal: {
        title: 'test_title',
        text: 'test_text',
        additionalText: 'test_additionalText',
      }
    }
  };
  notificationManager.publish(notificationRequest, (err: BusinessError) => {
    if (err) {
      hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
      return;
    }
    hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in publishing notification.');
  });
});
阅读 555
1 个回答

1、如果module.json5中的AbilityA的skills中仅配置了actions,则可以直接通过want-action做条件进行拉起。

{
  "name": "OtherAbility",
  "srcEntry": "./ets/otherability/OtherAbility.ets",
  "description": "$string:OtherAbility_desc",
  "icon": "$media:layered_image",
  "label": "$string:OtherAbility_label",
  "startWindowIcon": "$media:startIcon",
  "startWindowBackground": "$color:start_window_background",
  "launchType": "specified",
  "skills": [
    {
      "actions": ["actions.tpns.push"]
    }
  ]
}
let wantInfo: Want = {
  bundleName: "com.all.ly",//bundleName最好带上
  action: 'actions.tpns.push',
}
let context = getContext() as common.UIAbilityContext
context.startAbility(wantInfo)

2、如果module.json5中的AbilityA的skills中不仅需要配置了actions还需要配置uris,如果此时依然只希望通过actions进行拉起,则需要将actions和uris单独作为skills的子项进行配置,而不能仅作为一个子项进行配。

{
  "name": "OtherAbility",
  "srcEntry": "./ets/otherability/OtherAbility.ets",
  "description": "$string:OtherAbility_desc",
  "icon": "$media:layered_image",
  "label": "$string:OtherAbility_label",
  "startWindowIcon": "$media:startIcon",
  "startWindowBackground": "$color:start_window_background",
  "launchType": "specified",
  "skills": [
    {
      "actions": ["actions.tpns.push"]
    },
    {
      "uris": [
      {
        "scheme": "https",
        "host": "xxx",
      }
      ]
    }
  ]
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进