.NET MAUI App Links无法唤起应用,仅打开浏览器的问题排查
问题背景
已按微软文档配置.NET MAUI应用的深度链接/通用链接,iOS端更新版本号后功能正常,但Android端访问https://app.rackemapp.com仍仅打开浏览器。自定义schemerackemapp://可正常唤起应用,说明基础逻辑无问题,但设备未识别app.rackemapp.com的流量应导向应用。AASA文件测试正常,iOS通用链接已修复,Android端问题待解决。
排查与修复步骤
1. 修正assetlinks.json配置
你的assetlinks.json中namespace字段填写为RackEmAppMobile,不符合Android规范——该字段应与包名com.rackemapp.mobileapp一致,否则Google无法验证应用与域名的关联。
修正后的assetlinks.json:
[ { "relation": [ "delegate_permission/common.handle_all_urls" ], "target": { "namespace": "com.rackemapp.mobileapp", "package_name": "com.rackemapp.mobileapp", "sha256_cert_fingerprints": [ "C5:F1:29:4A:9D:95:41:F3:5A:0B:B9:DD:76:7A:F3:14:ED:D5:C0:17:D5:C8:F0:DB:B8:EC:70:E6:56:20:33:EC" ] } } ]
确保文件部署在https://app.rackemapp.com/.well-known/assetlinks.json,返回Content-Type为application/json,无重定向、404等错误。
2. 优化IntentFilter配置
当前MainActivity的IntentFilter中,Categories数组包含冗余的Intent.ActionView(已在Actions中定义),需清理以避免冲突:
修正后的MainActivity属性:
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] [IntentFilter(new[] { Android.Content.Intent.ActionView }, DataScheme = "https", DataHost = "app.rackemapp.com", DataPathPrefix = "/", AutoVerify = true, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable })] public class MainActivity : MauiAppCompatActivity { public MainActivity() { } }
3. 补充后台Intent处理逻辑
当前仅在应用启动时(OnCreate)处理Intent,但应用处于后台时,深度链接会触发OnNewIntent,需补充该生命周期的处理:
MauiProgram.cs中Android生命周期配置更新:
#if ANDROID lifecycle.AddAndroid(android => { android.OnCreate((activity, bundle) => { HandleAndroidIntent(activity.Intent); }); android.OnNewIntent((activity, intent) => { HandleAndroidIntent(intent); }); }); static void HandleAndroidIntent(Android.Content.Intent? intent) { var action = intent?.Action; var data = intent?.Data?.ToString(); if (action == Android.Content.Intent.ActionView && data is not null) { Task.Run(() => HandleAppLink(data)); } } #endif
4. 验证签名证书指纹
确保assetlinks.json中的sha256_cert_fingerprints与应用签名证书的指纹完全一致:
- 调试证书可通过以下命令获取指纹:
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android - 发布证书需使用对应keystore文件执行上述命令,确保指纹无大小写或符号错误。
5. 设备端验证与测试
- 清除浏览器缓存,避免旧跳转记录干扰测试。
- 进入设备设置 > 应用 > 你的应用 > 打开支持的链接,确认
app.rackemapp.com已被验证。 - 使用adb命令直接测试Intent,检查应用是否能接收链接:
adb shell am start -a android.intent.action.VIEW -d "https://app.rackemapp.com/test" com.rackemapp.mobileapp
额外注意事项
- 若使用Cloudflare等CDN,需确保
.well-known/assetlinks.json未被缓存,或设置短时间缓存策略。 - 发布版本应用必须使用与
assetlinks.json中指纹匹配的签名证书,调试版本可添加调试证书指纹到文件中(同时保留发布指纹)。
内容的提问来源于stack exchange,提问作者Matthew Warr

