How can I detect if my website is running inside a Trusted Web Actvity?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 02:02:29

问题


I'm opening my page inside a Trusted Web Activity and I want to detect when it's being opened inside it to customize behaviour and for analytics purposes. How can I detect that the page is being opened from the TWA?


回答1:


There are three options that will help detecting if the page is being opened from inside a TWA:

  1. When opening the page, the Referral will be android-app://<twa.package.name>, where twa.package.name is the package name used on the Android side of the TWA.

  2. Adding an URL parameter. Append a query string to the end of the URL that is launched with the PWA.

  3. Using Request-Headers. When creating the TWA Intent, add a Bundle containing the key/values for the a request header:

    Bundle headers = new Bundle();
    headers.putString("key", "value");
    customTabsIntent.intent.putExtra(Browser.EXTRA_HEADERS, headers);



回答2:


As andreban stated I used:

document.referrer.includes('android-app://')

which returns true if it comes from TWA.



来源:https://stackoverflow.com/questions/54580414/how-can-i-detect-if-my-website-is-running-inside-a-trusted-web-actvity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!