最全面的h5喚起app技術deeplink方案


前言

喚醒方式:

1、URL Schemes

2、android appLink

3、chrome intent

1、DeepLink實踐URL Schemes方式

a、需要在AndroidManifest.xml文件進行配置
<activity
    android:name=".ui.activity.SplashActivity"
    android:exported="true"
    android:screenOrientation="portrait"
    android:theme="@style/NormalSplash">
    
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    
    <!--DeepLink h5喚醒app配置-->
    <intent-filter>
        <!--ACTION_VIEW:支持被檢索-->
        <action android:name="android.intent.action.VIEW" />
        <!--CATEGORY_DEFAULT:響應隱式Intent-->
        <category android:name="android.intent.category.DEFAULT" />
        <!--CATEGORY_BROWSABLE:可被Web瀏覽器喚起-->
        <category android:name="android.intent.category.BROWSABLE" />
        <!--data:一個或多個,必須含有scheme標簽,決定被喚起的URL格式-->
        <data
            android:host="app.puxinwangxiao.com"
            android:scheme="pxwxstudent" />
        <!--    
        <data
            android:host="app.puxinwangxiao.com"
            android:scheme="pxwxstudent" 
            android:pathPrefix="/pxwx"/>
        <data
            android:host="app.puxinwangxiao.com"
            android:scheme="pxwxstudent" 
            android:path="/pxwx/user"/>
        -->
    </intent-filter>
</activity>
 

注意:

App可以配置多個支持喚起的Activity

Activity可以支持被多個URL喚起

若一個App配置了多個支持喚起的Activity,它們的scheme和host一般一致,然后通過path、pathPrefix等進行定向區分

b、被喚起后解析URL數據

Uri數據的解析可以在Activity中通過getIntent().getData()實現

@Override 
public void onCreate(Bundle savesInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    
    // 嘗試獲取WebApp頁面上過來的URL
    Uri uri = getIntent().getData();
    if (uri != null) {
        // scheme部分
        String scheme=data.getScheme();
        // host部分
        String host=data.getHost();
        // 訪問路徑
        String path=data.getPath();
        //參數
        Set<String> paramKeySet=data.getQueryParameterNames();
    }
}
 
c、在h5頁面上,通過如下方式使用:
<!--1.通過a標簽打開,點擊標簽是啟動-->
<!-- 注意這里的href格式 -- >
<a href="pxwxstudent://app.puxinwangxiao.com">open android app</a>

<!--2.通過iframe打開,設置iframe.src即會啟動-->
<iframe src="pxwxstudent://app.puxinwangxiao.com"></iframe>

<!--3.直接通過window.location 進行跳轉-->
window.location.href= "pxwxstudent://app.puxinwangxiao.com";
 
d、在原生App中喚起通過Intent方式
Intent intent = new Intent();
intent.setData(Uri.parse("pxwxstudent://app.puxinwangxiao.com/"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
 

2、DeepLink實踐Android AppLink方式

a、Android AppLink介紹

Android M以上版本可以通過AppLinks,讓用戶在點擊一個鏈接時跳轉到App的指定頁面;

前提是這個App已經安裝並經過驗證。

App Links的最大的作用,就是可以避免從頁面喚醒App時出現的選擇瀏覽器選項框;

前提是必須注冊相應的Scheme,就可以實現直接打開關聯的App。

Android App Links有以下幾點好處:

安全性/特殊性:由於Android App Links使用了HTTP/HTTPS URL的方式向開發者的服務器進行連接認證,所以其他應用無法使用我們的鏈接

無縫的用戶體驗:當用戶未安裝我們的應用時,由於使用的是HTTP/HTTPS URL,會直接打開一個網頁,我們可以在這個網頁中展示應用介紹等,而不是顯示404或者是其他錯誤頁面

支持Instant Apps:可以使用App Links直接打開一個未安裝的Instant App

支持Google Search或其他瀏覽器:用戶可以直接在Google Search/Google Assistant/手機瀏覽器/屏幕搜索中直接通過點擊一個URL來打開我們的指定頁面

b、Android AppLink集成

https://developer.android.com/studio/write/app-link-indexing.html

創建intent filter

我們在此處先假設用戶是通過http://resource.puxinwangxiao.com/pxwx來打開我們的應用的。

Android Studio 2.3以后提供了App Links Assistant來幫助開發者快速在AndroidManifest.xml中創建需要配置的intent filter,使用App Links Assistant有以下幾個步驟:

點擊Android Studio的菜單欄中的Tools > App Links Assistant

點擊Open URL Mapping Editor,然后在對話框底部點擊+去添加一個新的URL mapping

在彈出的Add URL Mapping對話框中輸入對應的內容,包括Host、Path、Activity, 輸入完成后點擊OK

注:App Link 支持多個域名

使用App Links Assistant在manifest文件中自動生成的內容如下:

<activity
            android:name=".ui.activity.SplashActivity"
            android:exported="true"
            android:screenOrientation="portrait"
            android:theme="@style/NormalSplash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                
            </intent-filter>
            
            <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="http"
                    android:host="resource.puxinwangxiao.com"
                    android:path="/pxwx" />
            </intent-filter>
        </activity>
 

檢查URL Mapping是否配置正確

App Links Assistant提供了檢查URL Mapping是否配置正確的快捷方式,操作如下:

點擊Open URL Mapping Editor,然后在Check URL Mapping對話框中輸入URL,當輸入一個能夠成功匹配到Acitivty的URL后,輸入框下方會顯示This URL maps to xxxxx(app)

處理App Links進入應用的場景

通過App Links Assistant -> Select Activity選擇之前配置的URL對應的Activity, 點擊Insert Code即可在onCreate方法中插入獲取從App Links跳轉而來的URL的代碼,生成代碼如下

// ATTENTION: This was auto-generated to handle app links.
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();

檢查assetlinks.json是否上傳成功

  • 為了在應用安裝成功后,系統能自動驗證該應用是否有權使用對應的域名,系統會向http://resource.puxinwangxiao.com/.well-known/assetlinks.json請求數據,根據獲取到的文件內容,驗證應用域名的合法性。

  • 通過App Links Assistant -> Open Digital Asset Links File Generator -> Generate Digital Asset Links file的方式生成assetlinks.json文件,然后將該文件放置到正確的域名地址中。

  • 通過App Links Assistant -> Open Digital Asset Links File Generator -> Generate Digital Asset Links file -> Link and Verify可以檢測是否正確的在服務器中放置配置文件,檢測成功的話,顯示如下圖:

我這里檢測后提示Network error.不影響 主要是http://resource.puxinwangxiao.com/.well-known/assetlinks.json能訪問到json文件

如果要讓網站和不同的應用關聯起來

網站可以在同一個assetlinks.json文件里聲明和不同的app的關系。下面這個文件列出了兩個聲明,這兩個聲明聲明了網站和兩個應用之間的關聯,這個文件位於https://app-pre.puxinwangxiao.com/.well-known/assetlinks.json。

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.pxwx.student",
    "sha256_cert_fingerprints":
    ["BD:EF:57:3D:01:D0:32:79:6E:32:73:18:32:E2:36:B9:35:1B:9C:7D:0F:F0:B0:A9:BE:91:18:CE:27:1A:D8:4C"]
  }
},
{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.pxwx.assistant",
    "sha256_cert_fingerprints":
    ["BD:EF:57:3D:01:D0:32:79:6E:32:73:18:32:E2:36:B9:35:1B:9C:7D:0F:F0:B0:A9:BE:91:18:CE:27:1A:D8:4C"]
  }
}]
 

注意:path、 pathPrefix、 pathPattern 之間的區別

例如:https://app-pre.puxinwangxiao.com/assistant/download.html

  • path 用來匹配完整的路徑,這里將 path 設置為 /assistant/download.html 才能夠進行匹配;

  • pathPrefix 用來匹配路徑的開頭部分,拿上面的 Uri 來說,這里將 pathPrefix 設置為 /assistant 就能進行匹配了;

  • pathPattern 用表達式來匹配整個路徑,這里需要說下匹配符號與轉義。

3、Chrome Intent方式實現從瀏覽器啟動應用

在很多應用中需要我們從瀏覽器中直接啟動應用,大多數采用的是上面提到的第一種scheme的方式,問題是如果手機中沒有應用,該url會跳轉到一個錯誤的界面。

google官方在chrome中推出了一種Android Intents的方式來實現應用啟動,通過在iframe中設置src為

intent:HOST/URI-path // Optional host
#Intent;
package=[string];
action=[string];
category=[string];
component=[string]; 
scheme=[string];
end;
 

mainfest文件中定義要啟動的activity

<activity
            android:name=".ui.activity.SplashActivity"
            android:exported="true"
            android:screenOrientation="portrait"
            android:theme="@style/NormalSplash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
            <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:host="app.puxinwangxiao.com"
                    android:scheme="pxwxstudent" />
            </intent-filter>
        </activity>

定義一個a標簽為

<a href="intent://app.puxinwangxiao.com/#Intent;scheme=pxwxstudent;package=com.xxx.xxx;end">open Android App</a>

在瀏覽器中點擊a標簽,就可以啟動應用程序的對應activity了.

如果手機中沒有相應的應用,防止跳轉到錯誤頁面,將a標簽設置為

<a href="intent://app.puxinwangxiao.com/#Intent;scheme=pxwxstudent;package=com.xxx.xxx;S.browser_fallback_url=https://www.puxinwangxiao.com;end">open Android App</a>

這樣如果沒有對應應用,該鏈接就會跳轉到S.browser_fallback_url指定的url上。

4、總結:

1、URL Scheme兼容性

URL Scheme只需要原生App開發時注冊Scheme即可,用戶點擊此類鏈接時,會自動喚醒App,並借助URL Router機制跳轉到指定頁面。

URL Scheme兼容性高,但卻存在許多限制:

  •  國內各個廠商瀏覽器差異很大,當要被喚醒的目標App未安裝時,這個鏈接很容易出錯。

  •  當注冊有多個Scheme相同的時候,目前是沒有辦法區分的。

  •  不支持從其他App中的UIWebView中跳轉到目標App。

  •  被部分主流平台禁止,微信、微博、QQ瀏覽器、手機百度中都已經被禁止使用。

由於這些限制的存在,安卓發布了自己的第二套方案:Android的App Links。

2、App Links兼容性
  •  App links在國內的支持還不夠,部分安卓瀏覽器並不支持跳轉至App,而是直接在瀏覽器上打開對應頁面。

  •  系統詢問是否打開對應App時,假如用戶選擇“取消”並且選中了“記住此操作”,那么用戶以后就無法再跳轉App。

3、chrome intent兼容性
  •  google通過chrome瀏覽器啟動的優化方案;

  •  很多第三方瀏覽器會攔截掉chrome intent啟動應用的請求

三種方案都有各自的兼容性,這幾項技術是基於系統平台的,每個系統版本的迭代后,配置方式都會有新的變化,國內的第三方平台openinstall也提供了專項功能,畢竟是專門做這個的,兼容性也都經受過市場驗證,懶得自己研究的,可以直接集成使用,參考下。

 

原文轉自:https://mp.weixin.qq.com/s/Cx9aIcfQVDaCqXV4atmIPQ

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM