Android 点击Url(短信链接)打开App

4,692 阅读2分钟

前言

先说一下要实现的效果吧: 在产品运营过程中时常会有一些消息或者活动通知到用户,且需要唤醒沉睡的用户 综合考量之下决定选用通过短信发送一条活动链接,通过点击这条链接可以直接跳转到我们的APP

用户收到短信-》点击短信-》已下载APP时直接打开APP,未下载时打开一个引导用户下载的网页地址

DEEP-LINK

在Android 系统中点击链接会发送一条 action = VIEW 的隐式意图 ,我们只需要在我们的APP 中加入相应的Intent 过滤器去满足这条规则即可

1、Intent 过滤器
<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="www.myapp.com" android:scheme="http" />
    <data android:host="www.myapp.com" android:scheme="https" />
</intent-filter>
1、 测试一下
  • 通过手机的短信、便签等方式通过点击地址(www.myapp.com)(http://www.myapp.…
  • 也可以通过命令行工具adb shell am start -W -a android.intent.action.VIEW -d "www.myapp.com"
  • 这样我们就实现了点击了一个链接,跳转到了我们的App
2、遇到的问题:

每次点击总是会出现一个弹框让我二次确认(一般是选择浏览器,只要是浏览器,都会相应http或者http开头的shceme,如果你的APP安装了多个浏览器,都会出现在这个弹框的选项中)

APP LINK

app link 是(Android 6.0)新增的一个特性,当用户点击一个链接时候,可以直接跳到我们的APP,不需要再通过弹窗进行二次确认

1、还是刚才那个Intent 过滤器,新增了android:autoVerify="true"属性(这段代码是通知Android系统,app在安装时需要对于安全host验证,验证通过后拉起app就不需要弹框安全提示,而是直接跳转app了。)
<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:host="www.myapp.com" android:scheme="http" />
    <data android:host="www.myapp.com" android:scheme="https" />
</intent-filter>
2、AndroidManifest添加meta-data
<meta-data android:name="asset_statements" android:resource="@string/asset_statements" />
3、strings.xml中添加值
<string name="asset_statements" translatable="false">
    [{
    "include": "https://www.myapp.com/.well-known/assetlinks.json"
    }]
</string>

或者

<string name="asset_statements">
  [{
    \"relation\": [\"delegate_permission/common.share_location\"],
    \"target\": {
      \"namespace\": \"web\",
      \"site\": \"https://example.com\"
    }
  }]
</string>
4、在host上放置一个加密文件

打开App Links Assistant

选第三项Associate website

这里要注意site domain必须是https的,没有的要注册一下https。

下面Preview就是安全验证文件内容,将这个文件丢到服务器上。

现在,applink已经可用了