转载请联系: 微信号: michaelzhoujayphp
原文请访问个人博客html
若是你的产品向用户提供网页服务,像 Web 页面或者为移动设备设计的 Html5 页面,那么我猜你 必定会鼓励用户将这些内容分享到其余平台,或者经过信息邮件分享。java
通常来讲产品经理会用各类机制来鼓励用户主动完成分享,有的产品会对完成分享的用户奖励, 好比积分、优惠券等。分享 的实质是基于用户关系的传播,让更多人接触到你的产品。这些看到 分享连接或者页面的人,若是产生一次点击,你须要尽一切可能把他转化成你的用户。提升点击连接 的效果,也就提升了产品的 分享转化率。android
因此本文主要解决的问题实际上是如何在 Android 上尽量提升分享转化率。git
这是后续步骤的基础,没有这个基础,后面说道的不少事情没有办法完成。 URL路由指的是你的 App 里的产品页面都须要能用户 URL 跳转。Github 上有很是多很是优秀的 URL 路由,像阿里巴巴技术团队的ARouter。 你只须要简单配置,加上注解,就能够很快的搭建本身的 URL 路由框架。github
下面咱们简单介绍一下基本原理。chrome
举个例子,一个新闻 App 提供 新闻详情页、新闻专题页、新闻讨论页 这个3个功能模块。 咱们先假设咱们要处理的 App 的包名为 com.zhoulujue.news
, 因此这些功能模块的链接 看起来应该是这样:数组
指向id=123456的新闻详情页:http://news.zhoulujue.com/article/123456/
指向id=123457的新闻专题页:http://news.zhoulujue.com/story/123457/
指向id=123456的新闻讨论页:http://news.zhoulujue.com/article/123456/comments/
复制代码
再假设这些页面的类名分别为:浏览器
新闻详情页:ArticleActivity
新闻专题页:StoryActivity
新闻讨论页:CommentsActivity
复制代码
因此咱们须要一个管理中心,完成两件事情:缓存
为了统一入口,咱们建立一个入口 Activity: RouterActivty
,它用来向系统声明 App 能 打开哪些连接,同时接受外界传递过来的 URL。首先咱们在 Manifest 里声明它:
<activity android:name=".RouterActivty" android:theme="@android:style/Theme.Translucent.NoTitleBar">
<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="news.zhoulujue.com" android:pathPattern="/.*" android:scheme="http" />
<data android:host="news.zhoulujue.com" android:pathPattern="/.*" android:scheme="https" />
</intent-filter>
</activity>
复制代码
上面的声明表示,RouterActivty 能够打开全部域名为news.zhoulujue.com 的 https/http 连接。 这个 RouterActivty 在收到 http://news.zhoulujue.com/article/123456/ 后,须要 负责将 /article/123456/ 解析出来,根据 对应关系 找到ArticleActivity
,唤起它而且 把123456
这个 id 做为参数传递给ArticleActivity
。
常见的 Router 框架经过在 Activity 的类名上添加注解来管理对应关系:
@Route(path = "/acticel/")
public class ArticleActivity extend Activity {
...
}
复制代码
实际上它在处理这个注解的时候生成了一个建造者模式里的 builder,而后向 管理中心 注册,说 本身(ArticleActivity)能处理/acticel/xxx
的子域名。
上面简述原理的时候说道了 Manifest 的声明,咱们只声明了 android:scheme="http"
和 android:scheme="http"
, 可是实际上不少 App 还会用特定 scheme
的方式来唤起 App,例如在 iOS 早期没有 UniversalLink 的时候,你们这样来唤起。
像淘宝就会用 tbopen
的 scheme,例如 tbopen://item.taobao.com/item.htm?id=xxxx
,当你在网页点击连接之后,页面会建立一个隐藏的 iframe,用它来打开自定义 scheme 的 URL,浏览器没法响应时,向系统发送一个 Action 为 android.intent.action.VIEW
、Data 为 tbopen://item.taobao.com/item.htm?id=xxxx
的 Intent,若是 App 已经按照上述章节改造,那么系统将唤起 RouterActivity 并将 Intent 传递过去。
因此问题就来了:如何选取一个 URL Scheme 使得“浏览器没法响应”,因此你的scheme 最好知足如下两个条件:
在咱们上述假设的新闻 App 里,咱们能够定义 scheme 为 zljnews
,那么在 URL Scheme 发送的 URL 将会是这样:
指向id=123456的新闻详情页:zljnews://news.zhoulujue.com/article/123456/
指向id=123457的新闻专题页:zljnews://news.zhoulujue.com/story/123457/
指向id=123456的新闻讨论页:zljnews://news.zhoulujue.com/article/123456/comments/
复制代码
为了不某些应用会预处理 scheme 和 host,咱们还须要将 URL Scheme 的 Host 也作相应 更改:
指向id=123456的新闻详情页:zljnews://zljnews/article/123456/
指向id=123457的新闻专题页:zljnews://zljnews/story/123457/
指向id=123456的新闻讨论页:zljnews://zljnews/article/123456/comments/
复制代码
这样的咱们的 Manifest 里 RouterActivity 的声明要改成:
<activity android:name=".RouterActivty" android:theme="@android:style/Theme.Translucent.NTitleBar">
<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="news.zhoulujue.com" android:pathPattern="/.*" android:scheme="http" />
<data android:host="news.zhoulujue.com" android:pathPattern="/.*" android:scheme="https" />
</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="zljnews" />
<data android:host="zljnews" />
<data android:pathPattern="/.*" />
</intent-filter>
</activity>
复制代码
咱们假设一个用例:用户在印象笔记里写了一篇笔记,笔记里有一个连接: http://news.zhoulujue.com/article/123456/
。 那么问题来了:用户点击之后,将会发生什么?
答案是:很大的多是系统弹出一个对话框,列出若干个 App,问你想用哪个打开。
这样体验其实不够好,由于用户路径变长了,转化率 将降低。因此咱们应该尽量去掉这个 对话框,其实上述章节说到了一个方法:将 http://news.zhoulujue.com/article/123456/
改成 zljnews://zljnews/article/123456/
,原理是咱们选取了看起来"惟一性"的 scheme, 可是若是用户没有安装你的 App,这个体验就至关糟糕了,用户在点击之后将没有任何反应。
此时就须要 AppLinks 和 UniversalLinks 了,一言以蔽之,就是域名持有者向系统证实本身 拥有 news.zhoulujue.com
这个域名而且 App 属于本身,这样系统就会直接将 App 唤起 并把 intent 传递给 App。
如何配置 AppLinks 就不在赘述了,参考官方的教程。
Facebook 在2014年的F8开发者大会上公布了 AppLinks 协议,在Android 的 AppLinks以前(Google I/O 15), 也是一种可行的“连接跳转 App”的方式。 这里也不在赘述细节,能够参考 Facebook 官方的介绍来实现,也特别简单:
上面说了不少在网页中唤醒 App 的方式,可是这些都是创建在咱们能够改页面 JS 等代码的前提下, 若是页面由第三方提供,举个例子,由广告主提供,表现方式是广告主提供一个落地页放在你的 App 里, 推进第三方去按照你的要求去改动他们的代码,可能比较困难,可是若是只是修改一下跳转连接就能够达到 唤起 App 的效果,这样性价比就比较高了。这个时候就须要 chrome 推荐的 intent scheme 了:
<a href="intent://zljnews/recipe/100390954#Intent;scheme=zljnews;package=com.zhoulujue.news;end"> Intent scheme </a>
复制代码
如代码所示,scheme填写的是咱们上面假设的 scheme:zljnews
,保持一致。 package 填写 App 包名:com.zhoulujue.news
,参考Chrome官方 Intent 编写规范
众所周知,微信是限制唤起 App 的行为的,坊间流传着各类微信唤起的 hack,但老是不知道何时就被封禁了,这里介绍 微信官方的 正规 搞法:微下载连接:
如上图,知乎就使用了微下载来向知乎的 App 导流,这种方式 Android iOS 都是通用的,具体实现方式参考腾讯微信官方的文档。
假设一个场景,用户访问 http://news.zhoulujue.com
阅读新闻时,被推荐下载了 App,此时安装完毕后打开 App后,最好 的体验固然是帮用户打开他没有看完新闻,直接跳转到刚刚在网页版阅读的文章。 最佳实践是:在用户点击下载时,把当前页面的 URL 写到 APK 文件的 ZIP 文件头里,待用户下载安装完毕后,启动时去读取这个 URL,而后结合上面说到过的 Router,路由到新闻详情页。下面跟我来一步一步实现吧。
将下面的 Java 代码保存为 WriteAPK.java
并用 javac 编译好。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.ZipFile;
/** * Created by michael on 16/9/8. */
public class WriteApk {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
if (args.length < 2) {
System.out.println("Wrong parameters! Usage : WriteApk path comment\n");
}
String path = args[0];
String comment = args[1];
writeApk(new File(path), comment);
System.out.println("Complete! File lies in " + path);
try {
ZipFile zipFile = new ZipFile(new File(path));
System.out.println("Zip file comment = " + zipFile.getComment());
} catch(IOException e) {
e.printStackTrace();
System.out.println("Zip file comment read failed!");
}
}
public static void writeApk(File file, String comment) {
ZipFile zipFile = null;
ByteArrayOutputStream outputStream = null;
RandomAccessFile accessFile = null;
try {
zipFile = new ZipFile(file);
String zipComment = zipFile.getComment();
if (zipComment != null) {
return;
}
byte[] byteComment = comment.getBytes();
outputStream = new ByteArrayOutputStream();
outputStream.write(byteComment);
outputStream.write(short2Stream((short) byteComment.length));
byte[] data = outputStream.toByteArray();
accessFile = new RandomAccessFile(file, "rw");
accessFile.seek(file.length() - 2);
accessFile.write(short2Stream((short) data.length));
accessFile.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
if (outputStream != null) {
outputStream.close();
}
if (accessFile != null) {
accessFile.close();
}
} catch (Exception e) {
}
}
}
/** * 字节数组转换成short(小端序) */
private static byte[] short2Stream(short data) {
ByteBuffer buffer = ByteBuffer.allocate(2);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putShort(data);
buffer.flip();
return buffer.array();
}
}
复制代码
而后使用下面的命令对 APK 写入 URL:
$java WriteAPK /path/to/your/APK http://news.zhoulujue.com/article/12345/
复制代码
在 App 首次打开的时候读取 ZIP 文件头里你写入的 URL,读取代码以下:
public static String getUnfinishedURL(Context context) {
//获取缓存的 APK 文件
File file = new File(context.getPackageCodePath());
byte[] bytes;
RandomAccessFile accessFile = null;
// 从指定的位置找到 WriteAPK.java 写入的信息
try {
accessFile = new RandomAccessFile(file, "r");
long index = accessFile.length();
bytes = new byte[2];
index = index - bytes.length;
accessFile.seek(index);
accessFile.readFully(bytes);
int contentLength = stream2Short(bytes, 0);
bytes = new byte[contentLength];
index = index - bytes.length;
accessFile.seek(index);
accessFile.readFully(bytes);
return new String(bytes, "utf-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (accessFile != null) {
try {
accessFile.close();
} catch (IOException ignored) {
ignored.printStackTrace();
}
}
}
return null;
}
复制代码
接着只要将getUnfinishedURL
返回值交给 Router 去处理,从而将用户导向没有阅读完毕的新闻详情页。
上面的内容都是在讲如何尽量地把用户导进 App 里来,从另一个角度,为了提升用户转化率咱们要下降用户的跳出率,也就是说尽可能避免用户从咱们的 App 里被带跑了。
不少状况下,若是咱们运营一个 UGC 的社区,咱们没法控制用户建立内容的时候会填写哪些 URL,固然做为一个开放的平台咱们确定但愿用户可以更高地利用各类工具将他们所专一的任务完成。
可是若是平台出现了一些人不受限制的发广告,或者利用你的平台运营竞争对手的产品,这种方式对成长中的产品打击有可能将是毁灭性的。
最佳实践:在服务器维护一个白名单,这个白名单中被容许的域名将被容许唤醒,不然拦截。
而这个拦截最好的方式是在WebView里,由于大多数跳转代码都在 URL 指向的落地页里。因此咱们须要这样定义WebView
的WebViewClient
public class ControlledWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Context context = view.getContext();
try {
String host = Uri.parse(url.getOriginalUrl()).getHost();
if (!isHostInWhiteList(host)) {
return false;
}
String scheme = Uri.parse(url).getScheme();
if (!TextUtils.isEmpty(scheme) && !scheme.equals("http") && !scheme.equals("https")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(intent);
return true;
}
} catch (Throwable t) {
t.printStackTrace();
}
return false;
}
private boolean isHostInWhiteList(String) {
// 查询白名单,是否在白名单里
...
}
}
复制代码
为了尽量获取正确的 Host,请注意在上面第7行代码里,使用的是url.getOriginalUrl()
好了,App 里面利用连接跳来跳去的事情基本上就讲完了,但愿对你有帮助。若是你还有什么建议,能够经过扫描下面的二维码联系我,或者在下面留言哦~