多來源受信任網路活動

Trusted Web Activities 是全新的方式,可讓您使用以「自訂分頁」為依據的通訊協定,將網頁應用程式內容 (例如 PWA) 與 Android 應用程式整合。

離線導覽

「信任的網路活動」需要使用 Digital Asset Links 開啟來源進行驗證,才能以全螢幕顯示內容。

當使用者離開已驗證的來源時,系統會顯示自訂分頁 UI。「Custom」分頁的網址列會告知使用者,目前正在瀏覽應用程式以外的網域,同時也提供 X 按鈕,讓使用者能快速返回已驗證的來源。

不過,網頁應用程式也時常建立跨多個來源的體驗。舉例來說,在 www.example.com 提供主要體驗的購物應用程式,結帳流程則由 checkout.example.com 代管。

在此情況下,不建議顯示「自訂分頁」,不僅因為使用者位於相同的應用程式,也可能因為頂端列可能會讓使用者認為已離開應用程式並放棄結帳。

受信任的網路活動可讓開發人員驗證多個來源,而使用者在瀏覽這些來源時,仍會以全螢幕模式顯示。與主網域一樣,開發人員必須能夠控管每個已驗證的來源。

為多個來源設定驗證

就像在主要來源中一樣,會透過 Digital Asset Links 進行驗證,而要驗證的每個網域都需有專屬的 assetlinks.json 檔案。

在此範例中,www.example.comcheckout.example.com 的值如下:

  • https://www.example.com/.well-known/assetlinks.json
  • https://checkout.example.com/.well-known/assetlinks.json

由於每個網域都會連結到同一個 Android 應用程式,因此 assetlinks.json 檔案看起來會完全相同。

假設 Android 應用程式的套件名稱為 com.example.twa,則這兩個 assetlink.json 檔案都會包含類似下列的內容:

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
  "namespace": "android_app",
  "package_name": "com.example",
   "sha256_cert_fingerprints": ["..."]}
}]

在 Android 應用程式中新增多個來源

在 Android 應用程式中,您需要更新 asset_statements 宣告,以納入所有需要驗證的來源:

<string name="asset_statements">
[{
    \"relation\": [\"delegate_permission/common.handle_all_urls\"],
    \"target\": {
        \"namespace\": \"web\",
        \"site\": \"https://www.example.com\"
    }
}],
[{
    \"relation\": [\"delegate_permission/common.handle_all_urls\"],
    \"target\": {
        \"namespace\": \"web\",
        \"site\": \"https://checkout.example.com\"
    }
}],
</string>

為 LauncherActivity 新增額外來源

使用預設 LauncherActivity

android-browser-helper 支援資料庫中的 LauncherActivity 可讓您設定 Android 專案,新增多個要驗證的來源。

首先,在 res/values/strings.xml 檔案中加入 string-array 元素。每個要驗證的額外網址都會位於 item 子元素中:

...
<string-array name="additional_trusted_origins">
    <item>https://www.google.com</item>
</string-array>
...

接著,在參照 LauncherActivityAndroidManifest.xml 現有活動元素中,新增一個 meta-data 標記:

...
<activity android:name="com.google.androidbrowserhelper.trusted.LauncherActivity"
    android:label="@string/app_name">


    <meta-data
        android:name="android.support.customtabs.trusted.ADDITIONAL_TRUSTED_ORIGINS"
        android:resource="@array/additional_trusted_origins" />


    ...
</activity>
...

使用自訂 LauncherActivity

使用自訂程式碼啟動受信任的網路活動時,您可以在建構意圖以啟動受信任網路活動時呼叫 setAdditionalTrustedOrigins 來新增額外來源:

public void launcherWithMultipleOrigins(View view) {
  List<String> origins = Arrays.asList(
      "https://checkout.example.com/"
  );


  TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder(LAUNCH_URI)
      .setAdditionalTrustedOrigins(origins);


  new TwaLauncher(this).launch(builder, null, null);
}

結論

完成這些步驟後,「Trusted Web Activity」就能支援多個來源。android-browser-helper 提供多個來源受信任網路活動 (Trusted Web Activities) 的範例應用程式。請務必仔細檢查。

疑難排解

設定 Digital Asset Links 需要幾個步驟。如果應用程式仍在頂端顯示「自訂分頁」列,表示設定可能有問題。

受信任網路活動快速入門指南提供了實用的疑難排解章節,說明如何偵錯 Digital Asset Link 問題。

另外,您也可以透過出色的 Peter 的 Asset Link 工具,對安裝在裝置上的應用程式對 Digital Asset Links 進行偵錯。