I am trying to implement a referral code system, and I am using Branch.io Metrics library. The problem I am facing is that the documentation is not good (doesn't work) and I am unable to generate a code
Documentation: https://github.com/BranchMetrics/Branch-Android-SDK#register-an-activity-for-direct-deep-linking-optional-but-recommended
Here are the steps I have taken including adding the library.
1) Grabbed the jar, added to my libs folder and added the following to my depenencies
compile files('libs/branch-1.5.9.jar')
2) In my application class that extends Application I added the following
if (DEBUG) {
Branch.getAutoTestInstance(this);
} else {
Branch.getAutoInstance(this);
}
3) In my AndroidManifest.xml I added the following
<meta-data android:name="io.branch.sdk.BranchKey" android:value="@string/bnc_app_key" />
4) In the Activity that I am testing everything, I added in the onStart() method the following
@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;
}
From the above, I believe I have successfully created a branch.io session and a listener that will allow me to retrieve data if branchError is null (there are no conflicts)
While still inside onStart() I now try to generate a referral code. So the whole onStart() looks as follows:
@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) I added onNewIntent override method
@Override
protected void onNewIntent(Intent intent) {
this.setIntent(intent);
}
My app does not reach inside of the onInitFinished listener, so I am unable to retrieve any code(s). Any suggestions on what I have missed is appreciated, and hopefully this thread will fill the holes that the documentation lacks.
I'm going to assume that the following lines of code are properly closed and aren't throwing syntax errors:
First, if you are extending the Application class, you need to initialize branch inside the
onCreate()
callback of your Application class:As per the Branch sample code found here, you actually need to call:
Instead of:
Inside your activities
onCreate
callback. Once that is taken care of, the Branch SDK will properly create.