Flutter接入华为App Linking如何获取链接参数且不启动新应用实例
解决Flutter App Linking参数获取问题
你拿到的https://myApp.drcn.agconnect.link/linkId是AGC生成的App Linking短链,本身没有明文携带配置的参数,直接通过app_links包读取短链地址自然无法获取参数,需要通过AGC App Linking SDK的解析接口读取参数,具体操作步骤如下:
1. 确认基础配置正确
你设置的singleTask启动模式已经可以避免重复打开应用实例,额外需要确认AndroidManifest.xml中已配置正确的intent-filter用于接收App Linking:
<intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" android:host="myApp.drcn.agconnect.link" /> </intent-filter>
2. 集成AGC App Linking Flutter SDK
在项目的pubspec.yaml中引入AGC App Linking官方SDK,完成后执行flutter pub get。
3. 解析短链获取参数
在app_links的链接监听回调中,调用AGC SDK的解析接口获取携带参数的深度链接:
import 'package:agconnect_applinking/agconnect_applinking.dart'; import 'package:app_links/app_links.dart'; late StreamSubscription<Uri> _appLinkSub; @override void initState() { super.initState(); final appLinks = AppLinks(); // 监听所有App Linking唤起事件,包括冷启动和后台唤起 _appLinkSub = appLinks.uriLinkStream.listen((shortLinkUri) async { try { // 调用AGC接口解析短链 final appLinkingResult = await AGCAppLinking.instance.getAppLinking(); final deepLink = appLinkingResult.deepLink; if (deepLink != null) { // 直接通过Uri的queryParameters获取所有参数 final paramMap = deepLink.queryParameters; // 示例:获取名为userId的参数 final userId = paramMap['userId']; // 此处执行对应业务逻辑 } } catch (e) { // 解析异常处理逻辑 } }); } @override void dispose() { _appLinkSub.cancel(); super.dispose(); }
注意事项
- 创建App Linking时需将参数配置在深度链接中,例如深度链接格式为
yourapp://detail?goodsId=123&userId=456,解析后即可从deepLink中读取到对应的goodsId、userId参数 - 禁止直接拆分短链地址提取参数,短链为AGC的中间跳转地址,参数存储在AGC后台,必须通过官方解析接口读取
内容的提问来源于stack exchange,提问作者Jonathan Ixcayau
相关产品推荐
相关产品推荐

