如何通过邮件中的Android deep link打开指定fragment并获取链接信息?
需求可行性判定
这个需求完全可实现,核心依赖系统的Deep Link(深度链接)能力,只需完成App路由规则配置、链接参数解析、Fragment跳转三个环节的开发即可。
具体实现流程
步骤1:配置App的Deep Link监听规则
你需要在App工程配置中,提前注册需要监听的链接格式,推荐优先使用HTTPS协议的链接(避免部分邮件应用拦截自定义scheme链接),比如你要使用的确认链接格式为https://your-app-domain.com/email-confirm?verifyCode=xxxx&uid=xxxx,对应配置如下:
安卓端配置
在负责接收Deep Link跳转的Activity的AndroidManifest.xml配置中添加intent-filter:
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 配置你要监听的HTTPS链接规则 --> <data android:scheme="https" android:host="your-app-domain.com" android:pathPrefix="/email-confirm" /> </intent-filter>
如果需要兼容未安装App时的网页跳转兜底,也可以额外添加自定义scheme的配置,比如scheme设为yourApp,对应链接格式为yourApp://email-confirm?xxx。
步骤2:解析Deep Link获取参数
用户点击链接唤起App后,系统会把完整的链接数据传入你配置的接收Activity,你可以在Activity的onCreate或onNewIntent生命周期中解析参数:
// 获取传入的链接Uri对象 val linkUri = intent?.data linkUri?.let { uri -> // 1. 获取完整链接内容 val fullLink = uri.toString() // 2. 获取指定查询参数 val verifyCode = uri.getQueryParameter("verifyCode") val userId = uri.getQueryParameter("uid") // 3. 判断路径匹配后跳转指定Fragment,把参数传入 if (uri.path == "/email-confirm") { val confirmFragment = EmailConfirmFragment() val bundle = Bundle() bundle.putString("verifyCode", verifyCode) bundle.putString("uid", userId) confirmFragment.arguments = bundle // 执行Fragment替换/跳转逻辑 supportFragmentManager.beginTransaction() .replace(R.id.app_container, confirmFragment) .addToBackStack(null) .commit() } }
注意事项
- 要做好异常兼容,比如链接参数为空、格式非法时,直接跳转App首页或默认页面,避免出现崩溃
- 如果使用HTTPS协议的Deep Link,建议配置Android App Link(安卓)或Universal Link(iOS)做域名权属校验,防止其他恶意App劫持你的链接跳转
- 邮件内容里的确认链接要和你App配置的规则完全匹配,否则无法正常唤起App
内容的提问来源于stack exchange,提问作者Kacper Królikowski
相关产品推荐
相关产品推荐

