关于android:h5唤起app技术deeplink方案总结

1次阅读

共计 7301 个字符,预计需要花费 19 分钟才能阅读完成。

前言

唤醒形式:

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 也提供了专项性能,毕竟是专门做这个的,兼容性也都禁受过市场重大,能够参考下。


关注我的技术公众号

正文完
 0