我正在尝试让 Android 深层链接仅适用于特定的 url,例如:
所以 http://books.com/about一直链接到网页
我的 AndroidManifest.xml
中的此配置有效并在我的应用程序中打开正确的页面。问题是这匹配 books.com 上的每一页
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/custom_url_scheme" />
<data android:scheme="https" android:host="books.com" />
</intent-filter>
当我把数据部分改成:
<data android:scheme="https" android:host="books.com" android:path="/book/nice-book-1" />
或
<data android:scheme="https" android:host="books.com" android:pathPrefix="/book" />
或
<data android:scheme="https" android:host="books.com" android:pathPattern="/book/..*" />
仅匹配 /book
后跟一个 slug,它根本不起作用。
我已经在 Stackoverflow 上发现了许多类似的问题,但它们要么已经过时,要么无法处理 url 与 slug 一起使用的情况。
- Android Navigation Deep Link - Ignore Query
- Intent filter using path,pathPrefix, or pathPattern
- How to use PathPattern in order to create DeepLink Apps Android?
有没有人在仅将几个 url 与 slug 匹配时遇到过同样的问题?
最佳答案
我花了很长时间才弄明白这一点。所以我会回答我自己的问题,以节省其他人宝贵的时间。
事实证明,当通过 Android studio 在设备上运行 Android 应用程序时
<data android:scheme="https" android:host="books.com" />
是唯一可行的路径。
使用时:
<data android:scheme="https" android:host="books.com" android:path="/book/nice-book-1" />
或
<data android:scheme="https" android:host="books.com" android:pathPrefix="/book" />
只有在您的设备上使用 SIGNED 版本的应用时,这些功能才有效。这可能是因为在您的 assetlinks.json
中有一个 sha256_cert_fingerprints
,它只能在使用已签名的应用程序时进行验证。
关于Android 深层链接路径、pathPrefix 或 pathPattern 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67947097/