HarmonyOS鸿蒙NEXT应用开发:Deeplink唤起应用商店如何传递参数?
HarmonyOS鸿蒙NEXT应用开发:Deeplink唤起应用商店如何传递参数? 如何给应用商店的安装包传渠道参数,比如需要把渠道等一系列参数传给 APP,该如何处理呢?
【背景知识】
拉起应用市场,跳转至应用详情界面,可以通过Deep Linking、App Linking和loadProduct三种方式实现。
【解决方案】
- Deep Linking:构造拼接bundleName的DeepLink链接,其中bundleName为需要打开的应用包名,其格式为:
store://appgallery.huawei.com/app/detail?id= + bundleName
注:为兼容旧链接, 可以在appId前拼接大写的C。如下所示:
// 比如123456是实际的appid。前面拼接上大写的C
uri: 'store://appgallery.huawei.com/app/detail?id=' + 'C123456'
在应用中调用context.startAbility()
方法,拉起应用市场应用详情页示例代码:
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import type { common, Want } from '@kit.AbilityKit';
// 拉起应用市场对应的应用详情页面
function startAppGalleryDetailAbility(context: common.UIAbilityContext, bundleName: string): void {
let want: Want = {
action: 'ohos.want.action.appdetail', // 隐式指定action为ohos.want.action.appdetail
uri: 'store://appgallery.huawei.com/app/detail?id=' + bundleName, // bundleName为需要打开应用详情的应用包名
};
context.startAbility(want).then(() => {
hilog.info(0x0001, 'TAG', "Succeeded in starting Ability successfully.")
}).catch((error: BusinessError) => {
hilog.error(0x0001, 'TAG', `Failed to startAbility.Code: ${error.code}, message is ${error.message}`);
});
}
@Entry
@Component
struct StartAppGalleryDetailAbilityView {
@State message: string = '拉起应用市场详情页';
build() {
Row() {
Column() {
Button(this.message)
.fontSize(24)
.fontWeight(FontWeight.Bold)
.onClick(() => {
const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
// 按实际需求获取应用的bundleName,例如bundleName: 'com.huawei.hmsapp.books'
const bundleName = 'xxxx';
startAppGalleryDetailAbility(context, bundleName);
})
}
.width('100%')
}
.height('100%')
}
}
在网页中打开DeepLink链接拉起应用市场应用详情页示例代码:
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
<button type="button" onclick="openDeepLink()">拉起应用详情页</button>
</div>
</body>
</html>
<script>
function openDeepLink() {
window.open('store://appgallery.huawei.com/app/detail?id=com.xxxx.xxxx')
}
</script>
在AppGallery Connect中使用“应用下载直达”功能,配置基于Deep Linking实现的图章链接,关联DeepLink,快速生成H5链接或二维码,用户通过点击H5链接或扫描二维码,即可拉起应用市场并跳转至应用详情,方便开发者推广应用:
- App Linking:构造拼接bundleName的App Linking链接,其中bundleName为需要打开的应用包名,其格式为:
https://appgallery.huawei.com/app/detail?id= + bundleName
在应用中调用openLink()
接口拉起App Linking链接的示例代码:
import type { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@Component
struct Index {
build() {
Button('start app linking', { type: ButtonType.Capsule, stateEffect: true })
.width('87%')
.height('5%')
.margin({ bottom: '12vp' })
.onClick(() => {
let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
// 需要拼接不同的应用包名,用以打开不同的应用详情页,例如:bundleName: 'com.huawei.hmsapp.books'
let bundleName: string = 'xxxx';
let link: string = 'https://appgallery.huawei.com/app/detail?id=' + bundleName;
// 以App Linking优先的方式在应用市场打开指定包名的应用详情页
context.openLink(link, { appLinkingOnly: false })
.then(() => {
hilog.info(0x0001, 'TAG', 'openlink success.');
})
.catch((error: BusinessError) => {
hilog.error(0x0001, 'TAG', `openlink failed. Code: ${error.code}, message is ${error.message}`);
});
})
}
}
在网页中打开App Linking链接的示例代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>跳转示例</title>
</head>
<body>
<a href='https://appgallery.huawei.com/app/detail?id=bundleName'>AppLinking跳转示例</a>
</body>
</html>
``
- 调用`loadProduct`方法拉起应用详情页面:
```javascript
aboutToAppear(): void {
const exposureData: productViewManager.SKExposure = {
adTechId: '20****e8',
campaignId: '123456',
destinationId: '10******',
mmpIds: ['2f****5', '2f7***5'],
serviceTag: '123***2',
nonce: '123***2',
timestamp: 1705536488,
signature: 'MEQCIEQlmZ****zKBSE8QnhLTIHZZZ****ZpRqRxHss65Ko****JgJKjdrWdkL****juEx2RmFS7da****ZRVZ8RyMyUXg=='
};
const uiContext = getContext(this) as common.UIAbilityContext
const wantParam: Want = {
parameters: {
// 必填,此处填入要加载的应用包名,例如: bundleName: 'com.huawei.xxx.xxx'
bundleName: 'com.xxx',
// 可选,向应用归因服务传递登记归因来源数据
skExposure: exposureData
}
}
const callback: productViewManager.ProductViewCallback = {
onError: (error: BusinessError) => {
hilog.error(0, 'TAG', `loadService onError.code is ${error.code}, message is ${error.message}`)
},
onAppear: () => {
hilog.info(0, 'TAG', `loadProduct onAppear.`);
},
onDisappear: () => {
hilog.info(0, 'TAG', `loadProduct onDisappear.`);
}
}
// 调用接口,拉起应用详情页
productViewManager.loadProduct(uiContext, wantParam, callback);
}
``
【常见FAQ】
Q:非公开发布应用,如何跳转至应用详情? A:应用选择[非公开发布](https://developer.huawei.com/consumer/cn/doc/app/agc-help-non-public-release-0000002047208149)上架后,发布类型下方展示非公开发布链接,链接形式为:`https://appgallery.huawei.com/app/detail?id=`包名,可以通过App Linking方式跳转。在已知应用包名的情况下也可以自行构造Deep Linking链接实现。
Q:是否支持自建应用市场? A:目前尚不支持自建应用市场,要下载更新需跳转华为应用市场,详情参考正文内容。
Q:构造DeepLink拉起应用下载安装的链接`store://enterprise/manifest?url=https://xxx.xxx/xxx.json5`,其中的url必须是公网可以访问的吗? A:是的。应用市场也会有下载校验。
Q:`productViewManager.loadProduct`调用的时候,会显示有个正在加载的动画图标,是否有参数控制可以去除? A:`productViewManager.loadProduct`没有参数控制去除正在加载的动画图标,可以使用DeepLink、App Linking接口,这两个接口没有加载的动画图标。
Q:请问这三种(loadProduct、DeepLink、App Linking方式)跳转应用市场的方式,有没有什么区别?更推荐哪一种? A:自研应用`loadProduct`可以配置拉起半屏详情页,非自研拉起全屏,显示在媒体进程中。applink和deeplink拉起的是全屏详情页,拉起AG进程,两种方式对比如下表:
| 方式 | 实现难度 | 安全性 | 用户体验 | 跳转方式 |
| --- | --- | --- | --- | --- |
| Deep Link | 相对简单 | 存在被恶意仿冒的风险 | 当目标应用未安装时,用户体验往往不佳,容易遇到报错情况 | 跳转时有弹窗,需要点击弹窗才能拉起app |
| App Linking | 在Deep Linking的基础上增加了域名校验环节,实现较复杂 | 提供了更高的安全性,避免了仿冒风险 | 提升了用户在应用间跳转时的整体使用体验,无论应用是否安装,用户都可以访问到链接对应的内容 | 可以直接拉起app |
基于以上区别,建议优先采用App Linking技术进行应用间跳转的开发。具体情况依据具体需求。
Q:我们设计了应用的下载页面,页面会展示当前我们在架应用的应用信息。HarmonyOS NEXT是否支持外部网页查询我们自己已上架应用的应用信息? A:可以使用Publishing API的[查询应用信息](https://developer.huawei.com/consumer/cn/doc/app/agc-help-publish-api-appinfo-query-0000002236041422)接口来查询应用信息。因为外部H5直接访问华为的接口,一般都会存在跨域问题。所以建议从开发者自己的服务端调用华为接口获取信息然后返回给前端H5页面。
更多关于HarmonyOS鸿蒙NEXT应用开发:Deeplink唤起应用商店如何传递参数?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS NEXT中,通过Deeplink唤起应用商店传递参数需使用URI参数拼接。示例格式:
appmarket://details?id=你的应用包名&参数名=参数值
关键点:
- 使用
appmarket://details
作为协议头 id
参数必须指定应用包名- 自定义参数以
&
符号追加
注意:参数值需进行URL编码,特殊字符需转义。应用商店版本需支持参数透传功能。
在HarmonyOS NEXT中,通过Deeplink唤起应用商店并传递参数给目标应用,可以通过以下方式实现:
- 使用标准的URL Scheme格式,例如:
hap://app/com.example.app?channel=test&source=web
- 参数传递的关键点:
- 参数需要以query string形式附加在URL后
- 应用商店会将完整URL传递给目标应用
- 目标应用需要在config.json中声明匹配的scheme
- 目标应用获取参数的代码示例:
import router from '@ohos.router'
// 在Ability的onCreate中获取参数
onCreate(want, launchParam) {
let uri = want.uri
if (uri) {
let params = new URL(uri).searchParams
let channel = params.get('channel')
// 处理参数...
}
}
- 注意事项:
- 确保参数经过URL编码
- 避免传递敏感信息
- 应用商店可能会对某些特殊字符进行过滤
这种方法适用于渠道跟踪、来源统计等场景,参数会随安装包一起传递给应用。