android - 受信任的 Web Activity : Not able to remove URL header

标签 android trusted-web-activity digital-assets-links

我正在探索受信任的 Web Activity 概念并尝试像这样启动它 -

final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
final CustomTabsIntent customTabsIntent = builder.build();    
customTabsIntent.intent.putExtra(TrustedWebUtils.EXTRA_LAUNCH_AS_TRUSTED_WEB_ACTIVITY, true);
customTabsIntent.launchUrl(activity, Uri.parse(wrappedTargetUrl));
我使用了 google 的 Assets 链接生成器 here并针对域进行了测试。它显示成功的链接。另外,我正在使用来自 Playstore 的正确应用签名 key 。尽管如此,它并没有为我隐藏 URL 栏。我已经查看并尝试了 StackOverflow 上的所有可用选项,但仍然没有运气。

最佳答案

要删除受信任的 Web Activity 的 URL 栏,它需要 Android 应用程序和网站之间的关联。必须以两种方式建立关联,链接从申请到网站从网站到应用程序 .
建立从应用程序到网站的关联
1.添加asset_statements字符串 app>res>values>strings.xml如下所示并更改站点属性的内容以匹配受信任的 Web Activity 打开的架构和域:

<resources>
    <string name="asset_statements">
        [{
            \"relation\": [\"delegate_permission/common.handle_all_urls\"],
            \"target\": {
                \"namespace\": \"web\",
                \"site\": \"https://my.site.com\"}
        }]
    </string>
</resources>
2.添加以上asset_statements进入你的 AndroidManifest.xml 为 meta-dataapplication 下像下面这样标记并设置正确的data scheme and host在您的启动器 Activity 中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.my.application">

    <application>

        <meta-data
            android:name="asset_statements"
            android:resource="@string/asset_statements" />

        <activity android:name=".LauncherActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>

           <!--
             This intent-filter allows the Trusted Web Activity to handle Intents to open the app
           -->
          <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"/>

            <!-- To handle links to the target URL-->
            <data
                android:scheme="https"
                android:host="my.site.com"/>
          </intent-filter>
       </activity>

    </application>
</manifest>
建立从网站到应用程序的关联
您必须上传 assetlinks.json文件到您的网站路径下:https://my.site.com/.well-known/assetlinks.json其中assetlinks.json 应该如下所示:
[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.example.my.application",
    "sha256_cert_fingerprints":
    ["5B:49:F7:CB:BL:C8:6K:CJ:5F:D8:33:2A:DA:FA:71:18:88:N9:49:9C:D3:66:98:35:B4:A2:2C:EC:B9:65:6C:65"]
  }
}]
从上面的文件你必须设置正确的包名 和正确的 SHA-256 指纹 ( keystore 签名哈希)。
有一种简单的方法可以生成上述 assetlinks.json file以及来自 Android Studio 的上述哈希值。你必须去 Android Studio Tools > App Links Assistant并点击Open Digital Asset Links File Generator并添加您的站点域、应用程序包名称并选择您的发布 keystore 文件,如下所示,然后单击 Generate Digital Asset Links file生成assetlinks.json的按钮已准备好上传到网站。还有一个非常有用的链接描述了该协会 Associate your app with your website
digital_asset_link
要验证链接是否正确,请使用 Link and Verify用于验证从网站到应用程序的关联是否正确的按钮,如下图示例所示:
link verification
在上述步骤之后,如果上传到站点的 Sha256 是用于发布 keystore 的,您必须生成一个发布 apk 文件,现在如果打开该文件,您应该会看到 Header Url Bar 已成功删除。我已经测试了上述步骤,它按预期工作。
注:
如果您选择让 Google Play 使用他们生成的 key 为您的版本签名,您只需复制 应用签名 key 证书 SHA-256 指纹 Google Play 控制台 -> 设置 -> 应用完整性 并将其放入assetlinks.json 文件中。

关于android - 受信任的 Web Activity : Not able to remove URL header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68638005/

相关文章:

android - 不同 session 中 PWA 的可信 Web Activity

android - 如何使用子域的通配符构建多源可信 Web Activity

Android:在安装时处理 App Link Verification 失败

Android Instant App Play 商店错误

android - 如何将多个 SHA-256 指纹添加到我的 assetslinks.json 以验证 Android 链接?

android - 无法正确地将我的 LinearLayout 更改为 ListView

java - 如何以编程方式更改样式属性值?

android - 如何在底部设置选项卡并隐藏顶部操作栏?

android - 在 Android APK 中打包多个项目

google-chrome - 受信任的 Web 事件 - Intranet/私有(private) Web 应用程序的数字 Assets 链接验证似乎失败