Flutter电商APP使用Deep Link与Intent实现支付回调相关配置疑问
电商APP Deep Link支付回调实现完整指导
前置说明
你的支付回调场景属于典型的Deep Link/App Link适用场景,以下针对你提出的4个配置问题逐一解答,最终给出可直接复用的配置方案。
问题1:<activity>标签的作用及给出代码的配置含义
<activity>是Android清单文件AndroidManifest.xml的核心组件声明标签,Android系统要求所有应用内的页面(Activity组件)必须在此处声明后才能被系统或其他应用调起。- 你给出的代码是Flutter项目默认的
MainActivity(应用入口页)配置,逐段解释如下:<activity android:name=".MainActivity" <!-- 指定当前Activity对应的代码类名,这里是Flutter默认入口 --> android:launchMode="singleTop" <!-- 启动模式,注意这里重复配置了launchMode,属于错误写法,建议保留singleTask即可 --> android:theme="@style/LaunchTheme" <!-- 指定启动页主题 --> android:configChanges="orientation|keyboardHidden|..." <!-- 配置所列属性变化时,不销毁重建Activity,由Flutter自行处理适配,避免页面重启 --> android:hardwareAccelerated="true" <!-- 开启硬件加速,提升页面渲染流畅度 --> android:windowSoftInputMode="adjustResize"> <!-- 软键盘弹出时自动调整页面尺寸,避免输入框被遮挡 --> android:launchMode="singleTask" <!-- 核心配置:启动模式设为singleTask,保证全局只有一个MainActivity实例,从浏览器回调时不会重复创建APP页面 --> <intent-filter> ...etc categories </intent-filter> </activity> - 适配建议:删除重复的
launchMode="singleTop",仅保留singleTask配置,满足Deep Link回调需求。
问题2:<action/>标签的作用及支付场景适配方案
<action/>是<intent-filter>下的动作匹配标签,用来声明当前Activity可以响应的系统动作类型,每一个<intent-filter>至少需要配置一个<action/>才能生效,系统发出的Intent只有匹配到对应动作才会继续校验其他属性。- 你给出的代码中各action的含义:
android.intent.action.VIEW:响应用户的查看请求,是Deep Link场景唯一需要的action,浏览器打开链接时发出的Intent默认携带该动作- 其余
BOOT_COMPLETED(开机启动)、INPUT_METHOD_CHANGED(输入法切换)、SEND/SEND_MULTIPLE(接收分享内容)均和支付回调场景无关,可直接删除
- 适配建议:你的场景仅需要保留以下action配置即可:
<action android:name="android.intent.action.VIEW" />
问题3:<category/>标签的使用方法及支付场景适配方案
<category/>是<intent-filter>下的类别匹配标签,用来给Intent增加额外的匹配规则,一个<intent-filter>可以配置多个<category/>,只有Intent携带的所有类别都能匹配成功,才会调起对应Activity。- 你给出的两行配置含义:
android.intent.category.DEFAULT:默认类别,所有隐式Intent(不指定具体调起哪个应用的哪个页面,比如浏览器打开链接的场景)都会被系统自动添加该类别,要让外部应用能调起你的Activity,必须配置该类别,否则无法匹配android.intent.category.BROWSABLE:浏览器可调用类别,浏览器发出的跳转Intent默认携带该类别,要实现从浏览器跳转回APP必须配置该类别
- 适配建议:你的支付回调场景刚好需要这两个类别,直接保留使用即可,不需要额外添加其他category配置。
问题4:<data/>标签的使用方法及支付场景配置方案
基础概念
<data/>是<intent-filter>下的数据匹配标签,用来匹配Intent携带的URI、MIME类型等数据,只有数据匹配成功才会调起对应Activity。- 核心属性说明:
scheme:URI的协议前缀,比如http/https/自定义的myapp都属于scheme,书写时不需要带://后缀,直接写协议名即可host:URI的域名部分,比如你的自有域名是pay.yourshop.com,host就填该值,不需要带http://前缀pathPrefix:可选配置,用来匹配URI的路径前缀,避免匹配域名下所有链接,只匹配支付回调相关路径mimeType:匹配接收的内容类型,你给出的各类text/*/image/*都是分享场景使用的,和支付回调无关,可直接删除
给出代码的含义
<!-- 匹配自定义scheme链接:myapp://example.com/xxx --> <data android:scheme="myapp" android:host="example.com" /> <!-- 匹配所有https开头的链接 --> <data android:scheme="https" /> <!-- 匹配所有http://flutterbooksample.com开头的链接 --> <data android:scheme="http" android:host="flutterbooksample.com" />
支付场景适配方案
第一步:配置data标签
你可以任选以下两种方案之一配置:
- 自定义Scheme方案(无需域名验证,跳转时会弹出打开方式选择框):
对应回调链接格式为:<data android:scheme="你自定义的协议名,比如myeshop" android:host="你可以自定义,比如pay.callback" />myeshop://pay.callback/result?status=success或myeshop://pay.callback/result?status=failure - App Link方案(使用你的自有域名,验证通过后直接跳转APP,无选择弹窗):
对应回调链接格式为:<data android:scheme="https" android:host="你的自有域名,比如pay.yourshop.com" android:pathPrefix="/payment/result" />https://pay.yourshop.com/payment/result?status=success或https://pay.yourshop.com/payment/result?status=failure
注意:该方案需要你在域名根目录下放置.well-known/assetlinks.json文件验证域名所有权,避免跳转到浏览器。
第二步:获取支付状态标识
Flutter层不需要写原生代码,直接使用你提到的receive_sharing_intent或uni_links包,监听传入的Intent URI,解析URI的query参数中status字段的值:
- 如果
status=success,跳转Payment_Successful_Page - 如果
status=failure,跳转Payment_Failed_Page
完整可复用的intent-filter配置示例
<activity android:name=".MainActivity" android:launchMode="singleTask" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <!-- Flutter默认启动intent-filter --> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <!-- 支付回调Deep Link intent-filter --> <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" /> <!-- 替换为你自己的scheme和host配置 --> <data android:scheme="myeshop" android:host="pay.callback" /> </intent-filter> </activity>
内容的提问来源于stack exchange,提问作者David Black
相关产品推荐
相关产品推荐

