关于android: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…

++创立intent filter++

咱们在此处先假如用户是通过==http://resource.puxinwangxiao…。

==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…,依据获取到的文件内容,验证利用域名的合法性。
  • 通过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…

如果要让网站和不同的利用关联起来

网站能够在同一个assetlinks.json文件里申明和不同的app的关系。上面这个文件列出了两个申明,这两个申明申明了网站和两个利用之间的关联,这个文件位于https://app-pre.puxinwangxiao…。

[{
  "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…

  • 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兼容性高,但却存在许多限度:

  • [x] 国内各个厂商浏览器差别很大,当要被唤醒的指标App未装置时,这个链接很容易出错。
  • [x] 当注册有多个Scheme雷同的时候,目前是没有方法辨别的。
  • [x] 不反对从其余App中的UIWebView中跳转到指标App。
  • [x] 被局部支流平台禁止,微信、微博、QQ浏览器、手机百度中都曾经被禁止应用。

因为这些限度的存在,安卓公布了本人的第二套计划:Android的App Links。

2、App Links兼容性
  • [x] App links在国内的反对还不够,局部安卓浏览器并不反对跳转至App,而是间接在浏览器上关上对应页面。
  • [x] 零碎询问是否关上对应App时,如果用户抉择“勾销”并且选中了“记住此操作”,那么用户当前就无奈再跳转App。
3、chrome intent兼容性
  • [x] google通过chrome浏览器启动的优化计划;
  • [x] 很多第三方浏览器会拦挡掉chrome intent启动利用的申请

三种计划都有各自的兼容性,这几项技术是基于零碎平台的,每个零碎版本的迭代后,配置形式都会有新的变动,国内的第三方平台openinstall也提供了专项性能,毕竟是专门做这个的,兼容性也都禁受过市场重大,能够参考下。


关注我的技术公众号

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理