我正在尝试实现推荐代码系统,并且正在使用 Branch.io Metrics 库。我面临的问题是文档不好(不起作用),我无法生成代码
这是我已采取的步骤,包括添加库。
1)抓住罐子,添加到我的libs文件夹中,并将以下内容添加到我的依赖项中
compile files('libs/branch-1.5.9.jar')
2)在我扩展应用程序的应用程序类中,我添加了以下内容
if (DEBUG) {
Branch.getAutoTestInstance(this);
} else {
Branch.getAutoInstance(this);
}
3)在我的 AndroidManifest.xml 我添加了以下内容
<meta-data android:name="io.branch.sdk.BranchKey" android:value="@string/bnc_app_key" />
4)在我正在测试所有内容的活动中,我在 onStart() 方法中添加了以下内容
@Override
protected void onStart() {
// note that branch is a global variable (Branch branch;)
if (DEBUG) {
branch = Branch.getTestInstance(this.getApplicationContext());
} else {
branch = Branch.getInstance(this.getApplicationContext());
}
branch.initSession(new BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
if (branchError == null) {}
}, this.getIntent().getData(), this;
}
从上面,我相信我已经成功创建了一个 branch.io 会话和一个侦听器,如果 branchError 为空(没有冲突),我将允许我检索数据
虽然仍在 onStart() 内部,但我现在尝试生成一个推荐代码。所以整个 onStart() 看起来如下:
@Override
protected void onStart() {
// note that branch is a global variable (Branch branch;)
if (DEBUG) {
branch = Branch.getTestInstance(this.getApplicationContext());
} else {
branch = Branch.getInstance(this.getApplicationContext());
}
branch.initSession(new BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
if (branchError == null) {}
}, this.getIntent().getData(), this;
}
branch.getReferralCode(5, new BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
try {
String code = jsonObject.getString("referral_code");
Log.d(TAG, "code: " + code);
} catch (JSONException e) {
Log.e(TAG, "JSONException :: " + e);
e.printStackTrace();
}
}
});
}
5)我添加了 onNewIntent 覆盖方法
@Override
protected void onNewIntent(Intent intent) {
this.setIntent(intent);
}
我的应用程序未到达 onInitFinished 侦听器内部,因此我无法检索任何代码。对我错过的任何建议表示赞赏,希望这个线程能够填补文档缺乏的漏洞。