我在我的Android应用程序中使用branch.io SDK,并希望使我的应用程序成为Android 6上分支链接的默认处理程序,如这里(Android指南)和这里(branch.io指南)所述
这是我在AndroidManifest.xml中的活动声明:
<activity android:name="com.mypackage.MyActivity"
android:launchMode="singleTask">
<intent-filter tools:node="merge" android:autoVerify="true">
<data android:scheme="@string/url_scheme" android:host="open"/>
<data android:scheme="https"
android:host="@string/branch_io_host"
android:pathPrefix="@string/branch_io_path_prefix"/>
<data android:scheme="http"
android:host="@string/branch_io_host"
android:pathPrefix="@string/branch_io_path_prefix"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
然而,当我在设备上安装构建时,当我单击具有正确主机和路径的链接时,我仍然会看到选择器对话框。在阅读了这份关于应用程序链接的广泛指南后,我相信这是因为我的设备从未验证过我的应用程序的意图过滤器。例如,当我从play store安装Twitter应用程序时,我会在LogCat:中看到这些消息
03-24 15:04:27.231: D/IntentFilterVerificationReceiver(16965): Received ACTION_INTENT_FILTER_NEEDS_VERIFICATION.
03-24 15:04:27.248: I/IntentFilterIntentService(16965): Verifying IntentFilter. verificationId:2 scheme:"https" hosts:"twitter.com www.twitter.com ads.twitter.com" package:"com.twitter.android".
03-24 15:04:30.134: I/IntentFilterIntentService(16965): Verification 2 complete. Success:true. Failed hosts:.
但当我安装应用程序时,我不会看到这样的消息。我尝试了发布和调试版本,尝试将其上传到play商店中的Alpha测试并从那里安装,结果相同。为什么Android不验证我的Intent过滤器?
通过将此数据标记移动到单独的意图过滤器中来修复此问题:
<data android:scheme="@string/url_scheme" android:host="open"/>
这就是AndroidManifest现在的样子:
<activity android:name="com.mypackage.MyActivity"
android:launchMode="singleTask">
<intent-filter tools:node="merge" android:autoVerify="true">
<data android:scheme="https"
android:host="@string/branch_io_host"
android:pathPrefix="@string/branch_io_path_prefix"/>
<data android:scheme="http"
android:host="@string/branch_io_host"
android:pathPrefix="@string/branch_io_path_prefix"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
<intent-filter>
<data android:scheme="@string/url_scheme" android:host="open"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
来自IntentFilter的消息现在按预期显示在日志中。
让Deeplink在没有用户提示的情况下工作
假设您的应用程序清单声明了多个主机,如myhost1.com、myhost2.com&myhost3.com,
<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="myhost1.com"
android:pathPrefix="/start" />
</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" />
<data
android:scheme="https"
android:host="myhost2.com"
android:pathPrefix="/start" />
</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" />
<data
android:scheme="https"
android:host="myhost3.com"
android:pathPrefix="/start" />
</intent-filter>
并为每个部署assetlinks.json
https://myhost1.com/.well-known/assetlinks.json
https://myhost2.com/.well-known/assetlinks.json
https://myhost3.com/.well-known/assetlinks.json
从ADB或Google Play商店安装应用程序时,请继续检查您的Logcat中的IntentFilter IntentOp标签,如下所示
I/IntentFilter IntentOp:正在验证IntentFilter。verificationId:9方案:";https";主机:";myhost1.com myhost2.com myhost3.com";包装:";com.mydeeplink.app";。[CONTEXT service_id=244]
安装时,如果ANYONE主机失败,则会验证清单声明。直接打开深度链接将无法工作。失败意味着将提示用户选择您的应用程序或浏览器。
注意注意注意:如果您在清单中声明任何将来将被删除的测试环境。这将导致应用程序的IntentFilter IntentOp失败。它将打破深层链接的直接打开。
对于稍后将被删除的测试环境,不要使用android:autoVerify=";真";。否则,新安装的生产应用程序深度链接直接打开可能会中断。
根据https://developer.android.com/training/app-links:
Android 6.0(API 23级)及更高版本上的Android应用程序链接允许应用程序将自己指定为给定类型链接的默认处理程序。
我碰巧在安卓5.1设备上随机测试了它,在安卓6以下,意向过滤器会起作用,但android:autoVerify="true"
不会。您将不会在adb logcat
中看到IntentFilterIntentSvc
日志,这意味着不会进行url验证。在这种情况下,Android将打开默认的应用程序选择面板。