為應用程式連結新增意圖篩選器

應用程式連結是使用 HTTP 或 HTTPS 配置的深層連結,且經過 Android 驗證與您的網站相關聯。如要註冊處理應用程式連結,請按照下列步驟操作:

  1. 在應用程式資訊清單中新增一或多個意圖篩選器,指定網站網域或網址。
  2. autoVerify="true"attribute 新增至意圖篩選器元素。這會向系統發出信號,表示應嘗試根據網站的 assetlinks.json 設定驗證架構和主機網域。
  3. 宣告網站關聯。

以下是應用程式連結宣告的範例,其中包含架構和主機,以及 autoVerify="true":

<activity
    android:name=".MainActivity"
    android:exported="true"
    ...>
    <!-- Make sure you explicitly set android:autoVerify to "true". -->
    <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" />

        <!-- If a user clicks on a link that uses the "http" scheme, your
             app should be able to delegate that traffic to "https". -->
        <!-- Do not include other schemes, as this will prevent verification. -->
        <data android:scheme="http" />
        <data android:scheme="https" />

        <!-- Include one or more domains that should be verified. -->
        <data android:host="www.example.com" />
        <data android:host="*.example.com" />
    </intent-filter>
</activity>

程式碼重點

  • AutoVerify:App Links 必須使用 android:autoVerify="true" 屬性。這會向系統發出信號,表示應嘗試驗證應用程式與 <data> 標記中指定的架構和網域之間的關聯。建議您為每個要驗證的意圖篩選器新增 autoVerify="true"。
  • 資料元素:每個應用程式連結意圖篩選器都必須包含一或多個 <data> 元素,指定與可驗證網站網域相符的架構和主機格式。
  • 配置:意圖篩選器必須包含 httphttps 配置的 <data> 元素。
  • 主機:您可以視需要新增 <data> 元素,以比對一或多個主機。使用萬用字元 (*) 比對多個子網域 (例如 *.example.com)。系統會嘗試根據您網站上的 assetlinks.json 檔案驗證每個主機。請注意,任何路徑層級的路由都應由 assetlinks.json 檔案處理 (請參閱下方的最佳做法一節)。

  • 多個主機:如果您宣告多個主機網域,系統 (Android 12 以上版本) 會嘗試驗證每個網域。如果驗證任何主機,應用程式就會成為該主機連結的預設處理常式。在 Android 11 以下版本中,只要有一個主機無法驗證,驗證就會失敗。

  • 多個意圖篩選器:如要宣告不重複的網址 (例如特定架構和主機組合),請務必建立個別篩選器,因為同一意圖篩選器中的多個 <data> 元素會合併,以反映組合屬性的所有變化版本。

資訊清單篩選規則注意事項

如果您要設定篩選器,以便在 Android 15 以上版本中使用動態應用程式連結,請務必注意,伺服器端 assetlinks.json 檔案中宣告的動態規則,無法擴大您在應用程式資訊清單中靜態宣告的網址規則範圍。

因此,我們建議採用下列做法:

  • 在應用程式資訊清單中,盡可能設定最廣泛的範圍,例如只宣告架構和網域
  • 依據伺服器端的 assetlinks.json 規則進一步調整,例如路徑層級的路由。

有了這個理想設定,您就能視需要動態新增 assetlinks.json 檔案中的應用程式連結路徑,並確知這些路徑會符合您在應用程式資訊清單中設定的廣泛範圍。

支援多個主機的應用程式連結

系統必須能夠根據該意圖篩選器中相應網域上託管的 Digital Asset Links 檔案,驗證應用程式網址意圖篩選器資料元素中指定的主機。如果驗證失敗,系統會預設採用標準行為來解析意圖,如「建立應用程式內容的深層連結」一文所述。不過,應用程式仍可驗證為應用程式其他意圖篩選器中定義的任何網址模式的預設處理常式。

舉例來說,如果系統在 https://www.example.com/.well-known/assetlinks.json 找到 assetlinks.json 檔案,但未在 https://www.example.net/.well-known/assetlinks.json 找到,則具有下列意圖篩選器的應用程式只會通過 https://www.example.com 的驗證:

<application>

  <activity android:name=”MainActivity”>
    <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" />
      <data android:scheme="http" />
      <data android:scheme="https" />
      <data android:host="www.example.com" />
    </intent-filter>
  </activity>
  <activity android:name="SecondActivity">
    <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="https" />
     <data android:host="www.example.net" />
    </intent-filter>
  </activity>

</application>

支援多個子網域的應用程式連結

Digital Asset Links 通訊協定會將意圖篩選器中的子網域視為獨立主機。因此,如果意圖篩選器列出多個具有不同子網域的主機,您必須在每個網域發布有效的 assetlinks.json。 舉例來說,下列意圖篩選器包含 www.example.commobile.example.com,做為可接受的意圖網址主機。因此,有效的 assetlinks.json 必須同時發布至 https://www.example.com/.well-known/assetlinks.jsonhttps://mobile.example.com/.well-known/assetlinks.json

<application>
  <activity android:name="MainActivity">
    <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" />
      <data android:scheme="https" />
      <data android:scheme="https" />
      <data android:host="www.example.com" />
      <data android:host="mobile.example.com" />
    </intent-filter>
  </activity>
</application>

或者,如果您使用萬用字元 (例如 *.example.com) 宣告主機名稱,則必須在根主機名稱 (example.com) 發布 assetlinks.json 檔案。舉例來說,如果應用程式具有下列意圖篩選器,只要 assetlinks.json 檔案發布在 https://example.com/.well-known/assetlinks.json,應用程式就能通過 example.com 任何子名稱 (例如 foo.example.com) 的驗證:

<application>
  <activity android:name="MainActivity">
    <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" />
      <data android:scheme="https" />
      <data android:host="*.example.com" />
    </intent-filter>
  </activity>
</application>

檢查是否有多個應用程式與同一個網域相關聯

如果您發布多個與相同網域相關聯的應用程式,這些應用程式都能順利通過驗證。不過,如果應用程式可以解析完全相同的網域主機和路徑 (例如應用程式的精簡版和完整版),只有最近安裝的應用程式可以解析該網域的網頁意圖。

在這種情況下,請檢查使用者裝置上是否有可能發生衝突的應用程式 (前提是您具備必要的套件瀏覽權限)。然後在應用程式中顯示自訂選擇器對話方塊,其中包含呼叫 queryIntentActivities 的結果。使用者可以從對話方塊中顯示的相符應用程式清單中,選取偏好的應用程式。