介绍
最有效的营销工具之一绝对是口耳相传。 当涉及到决策时,社会证明是一种非常有力的刺激手段。 尽管人们很容易忽略传统广告,但很少有人倾向于忽略来自可信赖来源(例如朋友)的建议。
2011年,Google和Ipsos OTX进行了一项调查,得出了有趣的结果。 如果推荐可以由收件人兑换的优惠券来补充,则他们下载该应用程序或将再次使用该应用程序的可能性更高。 这是开发人员和应用程序发布者可能要记住的事情。
事实是,很少有移动应用程序发行商和开发人员使用基于推荐的推荐系统。 原因几乎总是技术上的。 跟踪折扣代码,在应用程序内实现深层链接以及设置后端是开发人员进行此工作所需的一些障碍。 结果,大多数人不会打扰或放弃。
幸运的是,Google开发了克服这些障碍的系统,即App Invites 。 借助App邀请,Google提供了一个强大的界面,通过该界面,开发人员可以通过让现有用户邀请其联系人尝试该应用来扩大其移动应用的受众。
应用程序邀请包含在Google Play服务中,并且在iOS上也可用。 它通过减少摩擦并在用户邀请流程的每个步骤中提供一些上下文(例如,应用程序的一般信息及其在应用程序商店中的评级)来优化您的应用程序安装率。
在本教程中,我将向您展示如何在自己的Android应用程序中实现此功能强大的工具。
1.项目设置
步骤1:建立专案
我将使用一个非常简单的项目向您展示如何为您的应用共享邀请。 您需要做的第一件事是创建一个新项目。 在Android Studio中,选择“ 文件”>“新建”>“新建项目” 。 为您的应用命名,并设置公司的网络域,以让Android Studio定义您的应用的程序包名称。
该应用程序可以支持的最低SDK版本可以低至Android Gingerbread(API级别9)。 这意味着您可以针对几乎所有运行Android的设备,在撰写本文时,其定位目标都是99.97%。 但是,请记住,由于应用程序邀请基于Google Play服务插件,因此只能在包含Google Play商店的设备上使用。
生成一个空白的Activity
并将其命名为MainActivity.java 。
第2步:添加Google Play服务和支持库
在Android Studio中打开新项目后,打开左侧的“ 项目”标签,然后展开Gradle Scripts 。 它包含两个具有相同名称的文件build.gradle 。 第一个文件是顶级build.gradle 。 第二个文件是应用程序级别的build.gradle 。
打开顶级build.gradle并为Google Play服务添加以下classpath
。
classpath 'com.google.gms:google-services:1.5.0-beta2'
打开应用程序级别的build.gradle,并向其添加以下编译依赖项。
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.android.gms:play-services:8.3.0'
要启用Google Play服务插件,请在apply plugin: 'com.android.application'
下方添加以下行apply plugin: 'com.android.application'
。
apply plugin: 'com.google.gms.google-services'
Android Studio会要求您同步项目文件。 但是,在执行操作之前,请打开AndroidManifest.xml并在application
标记内添加Google Play服务版本。
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
保存更改,然后单击立即同步 。 我们还没有完成。 Android Studio会引发错误,告诉您Google Play服务插件需要配置文件google-services.json 。 让我们现在修复它。
2.添加配置文件
第1步:访问Google Developer Platform
您首先需要访问Google Developers网站才能为您的应用启用Google服务。 然后将该项目添加到您的个人开发者控制台,这意味着您必须先通过Google用户身份验证才能继续。
输入您为应用选择的名称,然后指定其程序包名称。 如果您不记得应用程序的软件包名称,请打开应用程序级别的build.gradle 。 您可以在defaultConfig> applicationId下找到它。
填写表格的其余部分,然后单击底部的“选择并配置服务 ”按钮。
步骤2:生成证书指纹
下一部分比较棘手。 Google会要求您提供证书指纹。 要获取证书指纹,您需要打开命令行并执行以下命令。
keytool -exportcert -list -v \
-alias <your-key-name> -keystore <path-to-production-keystore>
有两种可能的情况:
- 您正在本地测试您的应用程序。
- 您正在Google Play开发者控制台上部署APK。
如果您正在本地测试应用程序,则密钥名称为androiddebugekey
,其默认位置在Windows上为%USERPROFILE%\.android\debug.keystore
在Windows上为~/.android/debug.keystore
在Unix系统上(例如OS X和Linux) 。 这是用于本地测试的命令。
keytool -exportcert -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore
keytool实用程序将提示您输入密码。 调试密钥库的默认密码是android 。 这将产生调试证书的SHA1指纹,看起来像这样。
Certificate fingerprint: SHA1: DA:39:A3:EE:5E:6B:4B:0D:32:55:BF:EF:95:60:1H:90:AF:D8:07:09.
但是,如果需要发布证书指纹,则需要指定证书的名称,位置和密码。
步骤3:添加配置文件
输入证书指纹后,您可以通过单击底部的“ 启用应用程序邀请”按钮来启用应用程序邀请 。 现在,您应该可以通过点击下载google-services.json下载配置文件。
要完成此步骤,请将下载的文件复制到项目的app /或mobile /目录。
3.发送邀请
步骤1:定义版面
配置项目后,我们就可以进行下一步了。 对于启动程序Activity
,定义一个简单的布局,如下所示。 它包含一个按钮和一个标签。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Hi."
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textSize="45sp" />
<Button
android:id="@+id/invite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100dp"
android:colorButtonNormal="#3F51B5"
android:text="Wanna bring in your friends?"/>
</RelativeLayout>
结果应如下所示。
在MainActivity
类的onCreate
方法中,将onCreateListener
添加到按钮。
button = findViewById(R.id.invite);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something, will you?
}
});
邀请将作为Intent
发送。 您可以指定一条消息,用户可以在将邀请发送给他们的朋友之前对其进行自定义。 您还可以设置号召性用语,背景图片以及应用收到邀请后您的应用即可处理的深层链接。 您甚至可以定义最低SDK版本( setAndroidMinimumVersionCode
)和其他平台(例如iOS)的目标应用程序ID( setOtherPlatformsTargetApplication
)。
在onClick
方法的主体中创建Intent
。
Intent intent = new AppInviteInvitation.IntentBuilder(INVITATION_TITLE)
.setMessage(INVITATION_MESSAGE)
.setCallToActionText(INVITATION_CALL_TO_ACTION)
.build();
startActivityForResult(intent, REQUEST_INVITE);
添加以下字符串以完成所有这些工作。
private static final
String INVITATION_TITLE = "Call your friends",
INVITATION_MESSAGE = "Hey! Would you like to get a 50% discount for this awesome app? :)",
INVITATION_CALL_TO_ACTION = "Share";
这里的所有都是它的。 启动Intent
,将显示一个自定义Activity
,用户可以通过该Activity
选择发送邀请(电子邮件或短信),邀请消息和收件人的方式。
通过调用startActivityForResult
来启动此意图。 可以获取意图的结果。 一旦用户点击了发送按钮,他们就会被带回到开始意图的活动中。 onActivityResult
,您可以在onActivityResult
执行任何必要的操作,例如隐藏按钮或通过分析跟踪邀请。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_INVITE) {
if (resultCode == RESULT_OK) {
// You successfully sent the invite,
// we can dismiss the button.
button.setVisibility(View.GONE);
String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
StringBuilder sb = new StringBuilder();
sb.append("Sent ").append(Integer.toString(ids.length)).append(" invitations: ");
for (String id : ids) sb.append("[").append(id).append("]");
Log.d(getString(R.string.app_name), sb.toString());
} else {
// Sending failed or it was canceled using the back button
showMessage("Sorry, I wasn't able to send the invites");
}
}
}
4.深层链接
步骤1:发送深层链接数据
当用户收到邀请并单击包含的链接时,邀请流程会根据收件人的智能手机上是否已安装该应用程序进行分支。 如果该应用程序已经存在,它将自动接收包含可选深层链接数据的Intent
。
但是,如果收件人没有安装该应用程序,则可以给他们机会。 首次启动该应用程序时,该应用程序将收到包含深层链接数据的Intent
。 这样,您可能想提供的折扣将始终对收件人可用。 这克服了我们前面讨论的问题。
让我们更详细地探讨深层链接。 设置我们在onCreate
方法中创建的Intent
的深层链接。
Intent intent = new AppInviteInvitation.IntentBuilder(INVITATION_TITLE)
.setMessage(INVITATION_MESSAGE)
.setDeepLink(Uri.parse("tutsplus://code.coupon/50"))
.setCallToActionText(INVITATION_CALL_TO_ACTION)
.build();
步骤2:针对自定义网址的意图过滤
要处理自定义深层链接,您需要做的第一件事就是告诉您的应用程序它们是如何制作的,以及哪个Activity
应该照顾它们。 将新的Activity
添加到您的项目中,并将其命名为DetailsActivity 。
打开AndroidManifest.xml并将以下代码段添加到<activity android:name=".DetailsActivity">
标记中。
<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="code.coupon"
android:scheme="tutsplus" />
</intent-filter>
我们告诉系统以下内容。 如果它在方案为tutsplus且宿主为code.coupon的URL上调用操作VIEW ,则您的应用可以使用DetailsActivity
类来处理它。
为了接收和解析自定义URL,首先必须创建一个具有对App邀请的访问权限的自动管理的GoogleApiClient
。 我们在MainActivity
类的onCreate
方法中执行此操作。
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(AppInvite.API)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(getString(R.string.app_name), "onConnectionFailed:" + connectionResult);
showMessage("Sorry, the connection has failed.");
}
})
.build();
步骤3:接收和解析深层链接
要检查邀请并启动深层链接Activity
,可以使用getInvitation
方法,该方法是Google Play服务8.3中引入的。 将以下代码片段添加到MainActivity
类的onCreate
方法中。
AppInvite.AppInviteApi.getInvitation(googleApiClient, this, true)
.setResultCallback(
new ResultCallback<AppInviteInvitationResult>() {
@Override
public void onResult(AppInviteInvitationResult result) {}
});
将方法的最后一个参数设置为true
告诉插件自动调用负责处理深层链接的活动,在我们的示例中为DetailsActivity
类。 这就是为什么onResult
方法的主体为空的原因。
打开activity_details.xml并添加两个TextView
对象,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.tutsplus.code.appinvites.DetailsActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Awesome!"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textSize="45sp" />
<TextView
android:id="@+id/discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100dp"
android:gravity="center"
android:text="You accepted the invitation, and this gives you the right to a %s percent discount :)"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="20sp" />
</RelativeLayout>
接下来,打开DetailsActivity
并覆盖onStart
方法。 要检查Intent
包含深层链接数据,您必须依靠hasReferral
方法。 如下所示,URL解析非常简单。
@Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
if (AppInviteReferral.hasReferral(intent)) {
// Extract the information from the Intent
String deepLink = AppInviteReferral.getDeepLink(intent);
Log.d(getString(R.string.app_name),
"Found Referral: " + AppInviteReferral.getInvitationId(intent) + ":" + deepLink);
String[] array = deepLink.split("/");
if (array.length > 0) {
TextView tv = (TextView) findViewById(R.id.discount);
tv.setText(String.format(tv.getText().toString(), array[array.length-1]));
}
}
}
结论
在本文中,您学习了如何使用Google Play服务插件在应用中实现Google的应用邀请。 此工具可让用户向其Google联系人发送邀请,从而增加应用的受众群体。 现在,您应该可以利用应用邀请来开发推荐系统,让用户使用个性化的上下文邀请向您的朋友推荐您的应用。
翻译自: https://code.tutsplus.com/tutorials/how-to-grow-your-audience-with-google-app-invites--cms-25321