简体   繁体   English

PWA 与 TWA:如何强制使用 Chrome 而不是默认浏览器

[英]PWA with TWA: How to force Chrome instead of default browser

I have built a PWA with TWA & generated APK following the official Google guide here - https://developers.google.com/web/updates/2019/02/using-twa我已经按照此处的官方 Google 指南使用 TWA 构建了 PWA 并生成了 APK - https://developers.google.com/web/updates/2019/02/using-twa

What's happening is that when a different browser apart from Chrome is set as default, the behaviour is unpredictable.发生的情况是,当除 Chrome 之外的其他浏览器设置为默认浏览器时,行为是不可预测的。 For example: on a Xiaomi phone where MI Browser is the default, my app works just as a shortcut & the page just loads in one of the tabs of the browser.例如:在默认 MI 浏览器的小米手机上,我的应用程序作为快捷方式工作,页面仅加载到浏览器的一个选项卡中。 This phone had Chrome installed, but my app still used the default browser to render my PWA.这款手机安装了 Chrome,但我的应用程序仍然使用默认浏览器来呈现我的 PWA。

Official documentation states the following:官方文档说明如下:

Today, if the user's version of Chrome doesn't support Trusted Web activities, Chrome will fall back to a simple toolbar using a Custom Tab.今天,如果用户版本的 Chrome 不支持受信任的 Web 活动,Chrome 将退回到使用自定义选项卡的简单工具栏。 It is also possible for other browsers to implement the same protocol that Trusted Web activities use.其他浏览器也可以实现可信 Web 活动使用的相同协议。 While the host app has the final say on what browser gets opened, we recommend the same policy as for Custom Tabs: use the user's default browser, so long as that browser provides the required capabilities.虽然主机应用程序对打开的浏览器有最终决定权,但我们建议使用与自定义选项卡相同的策略:使用用户的默认浏览器,只要该浏览器提供所需的功能。

While the guide has this paragraph, I'm unable to find any documentation on how to set a preferred browser for my PWA虽然指南有这一段,但我找不到任何关于如何为我的 PWA 设置首选浏览器的文档

To open URL in Chrome i'm using Chrome custom tabs from androidx.browser.browser:1.0.0 First i check if Chrome custom tabs are supported要在 Chrome 中打开 URL,我使用来自androidx.browser.browser:1.0.0 Chrome 自定义标签首先我检查是否支持 Chrome 自定义标签

const val SERVICE_ACTION = "android.support.customtabs.action.CustomTabsService"
const val CHROME_PACKAGE = "com.android.chrome"

private fun Context.isChromeCustomTabsSupported(): Boolean {
    val serviceIntent = Intent(SERVICE_ACTION)
    serviceIntent.setPackage(CHROME_PACKAGE)
    val resolveInfos = packageManager.queryIntentServices(serviceIntent, 0)
    return !(resolveInfos == null || resolveInfos.isEmpty())
}

Then open URL in custom tab然后在自定义选项卡中打开 URL

if (isChromeCustomTabsSupported()) {
        CustomTabsIntent.Builder().apply {
            setToolbarColor(ContextCompat.getColor(this@MainActivity, R.color.colorPrimary))
            setSecondaryToolbarColor(ContextCompat.getColor(this@MainActivity, R.color.colorPrimaryDark))
        }.build().launchUrl(this@MainActivity, Uri.parse(URL))
    }

If the answer in Kotlin is not OK, i will rewrite in Java如果Kotlin的答案不好,我会用 Java 重写

Forcing Chrome as the browser to handle the Trusted Web Activity is not recommended , as the number of browsers that support Trusted Web Activity is growing and developers should try to honour the user's browser choice as much as possible .不建议强制使用 Chrome 作为浏览器来处理 Trusted Web Activity ,因为支持 Trusted Web Activity 的浏览器数量正在增长,开发人员应尽量尊重用户的浏览器选择

The implementation that chooses the browser in android-browser-helper will:android-browser-helper中选择浏览器的实现将:

  1. Look for browsers that support TWAs, if you find one, pick it.寻找支持 TWA 的浏览器,如果找到,请选择它。
  2. Otherwise, look for browser that support Custom Tabs, if you find one, pick it.否则,请寻找支持自定义选项卡的浏览器,如果找到,请选择它。
  3. Otherwise launch to the default browser.否则启动到默认浏览器。

Alternatively, developers can choose to replace number 2 and 3 with a WebView fallback implementation , even though there are tradeoffs on the supported feature set.或者,开发人员可以选择用WebView 回退实现替换数字 2 和 3,即使在支持的功能集上存在折衷。

Regarding the below:关于以下内容:

on a Xiaomi phone where MI Browser is the default, my app works just as a shortcut & the page just loads in one of the tabs of the browser.在默认 MI 浏览器的小米手机上,我的应用程序就像一个快捷方式,页面只是在浏览器的一个选项卡中加载。 This phone had Chrome installed, but my app still used the default browser to render my PWA.这款手机安装了 Chrome,但我的应用程序仍然使用默认浏览器来呈现我的 PWA。

There are a few things that may be happening here:这里可能会发生一些事情:

  1. The Chrome version installed is not up to date and doesn't support Trusted Web Activity, so it's falling back to (2)安装的 Chrome 版本不是最新的,不支持可信网络活动,所以它回退到 (2)
  2. The MI browser declares to support Trusted Web Activity, but doesn't really. MI 浏览器声明支持受信任的 Web 活动,但实际上并非如此。 This seems to be the case on Kindle Fire devices . Kindle Fire 设备似乎就是这种情况
  3. A possible bug on the logic that chooses the browser.选择浏览器的逻辑可能存在错误。

For 1, the solution is updating Chrome and everything should work.对于 1,解决方案是更新 Chrome,一切正常。 Otherwise, enable the WebView fallback.否则,启用 WebView 回退。

For 2, it sounds like what is needed is a "disallow list" to avoiding using browsers that are known to declare support but don't.对于 2,听起来需要一个“禁止列表”来避免使用已知声明支持但不支持的浏览器。 I'd recommend filing a feature-request at https://github.com/GoogleChrome/android-browser-helper/我建议在https://github.com/GoogleChrome/android-browser-helper/提交功能请求

For 3, file a bug at https://github.com/GoogleChrome/android-browser-helper/对于 3,在https://github.com/GoogleChrome/android-browser-helper/提交错误

Update: I installed MI Browser with a project generated via Bubblewrap .更新:我通过Bubblewrap生成的项目安装了MI 浏览器 MI Browser doesn't declare support for Trusted Web Activity and Chrome is opened as expected. MI 浏览器未声明支持 Trusted Web Activity,Chrome 已按预期打开。 I'm leaning towards the problem being related to (1) above.我倾向于与上述(1)相关的问题。

Finally, it is possible to implement your own provider picker for Trusted Web Activity, where you could maintain a "disallow list" while an automated way is not implemented in Android Browser Helper.最后,可以为受信任的 Web 活动实现您自己的提供程序选择器,您可以在其中维护“禁止列表”,而在 Android 浏览器助手中未实现自动化方式。 See this sample for details.有关详细信息,请参阅此示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 适用于 Chrome 和三星 Internet 的 TWA 全屏 PWA - TWA Fullscreen PWA for Chrome and Samsung Internet 如何让我的 Android 应用程序始终在 Chrome 或其他支持 TWA 的浏览器中打开? - How can I get my Android app to always open in Chrome or another browser that supports TWA? 使用 bubblewrap 生成的 TWA 应用程序在浏览器中打开,而不是作为应用程序打开 - TWA app generated using the bubblewrap opens in browser instead of as an app 如何强制文件在应用内下载,而不是显示在默认浏览器中 - How to force a file to download in-app instead of showing up in the default browser 如何强制Webview应用在其中打开链接,而不是根据域在默认的Android浏览器中打开链接? - How can I force a webview app to open links in it instead of open them in the default android browser depending on the domain? 当设备中安装了多个浏览器并且用户将其他浏览器设置为默认浏览器而不是chrome浏览器时,如何加载chrome自定义标签? - How to load chrome custom tab when multiple browsers installed in the device and user has set different browser as default instead of chrome browser? 如何在 Android 设备上的 TWA 应用程序上隐藏“在 Chrome 中运行”吐司? - How to hide 'Running in Chrome' toast on TWA application on Android devices? 在未安装浏览器的情况下运行 TWA - Run TWA without a browser installed 如何强制碎片使用默认浏览器? - How can I force splinter to use a default browser? 使用TWA在Google Play上成功发布的PWA行为异常 - PWA successfully published on Google Play using TWA has an abnormal behave
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM