How to implement the Referral system with Branch.i

2020-03-31 06:38发布

问题:

I've followed the documents, I don't know what's wrong, I would really appreciate some insight.

Manifest:

<uses-permission android:name="android.permission.INTERNET" />

<application
    ...
    android:name="io.branch.referral.BranchApp">
    <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_123456789asdf" />
    <activity
        ...>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter android:label="indexed_on_SEO">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:scheme="http"
                android:host="www.schoolminder.info"
                android:pathPrefix="/home"/>
        </intent-filter>
        <intent-filter>
            <data
                android:scheme="http"
                android:host="open" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    </activity>

    <receiver
        android:name="io.branch.referral.InstallListener"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>

</application>

The activity

@Override
protected void onStart() {
    super.onStart();
    Branch branch = Branch.getInstance();

    branch.initSession(new Branch.BranchReferralInitListener(){
        @Override
        public void onInitFinished(JSONObject referringParams, BranchError error) {
            if (error == null) {
                Log.i("MyApp", "deep link data: " + referringParams.toString());
            } else {
                Log.i("MyApp", error.getMessage());
            }
        }
    }, this.getIntent().getData(), this);

    String userID = new BigInteger(130, new SecureRandom()).toString(32);
    Branch.getInstance().setIdentity(userID);
}

@Override
protected void onNewIntent(Intent intent) {
    this.setIntent(intent);
}

The fragment when the invitation is made:

    BranchUniversalObject branchUniversalObject = new BranchUniversalObject()
            .setCanonicalIdentifier("item/12345")
            .setContentIndexingMode(BranchUniversalObject.CONTENT_INDEX_MODE.PUBLIC)
            .addContentMetadata("property1", "blue")
            .addContentMetadata("property2", "red");

    LinkProperties linkProperties = new LinkProperties()
            .setChannel("facebook")
            .setFeature("sharing");

    branchUniversalObject.generateShortUrl(getActivity(), linkProperties, new Branch.BranchLinkCreateListener() {
        @Override
        public void onLinkCreate(String url, BranchError error) {
            if (error == null) {
                link = url;
            }
        }
    });

    Branch.getInstance(getActivity().getApplicationContext()).loadRewards(new                                   Branch.BranchReferralStateChangedListener() {
        @Override
        public void onStateChanged(boolean changed, BranchError error) {
            int credits = Branch.getInstance().getCredits();
            Branch.getInstance().redeemRewards(credits);
            Log.i("MyApp", credits+"");
        }
    });

I dealt with the links for each platform on the Branch Dashboard settings.

What's happening is, when I send this link to someone, it opens the Play Store as expected and he can download it, but I don't get the credits, and it doesn't show any Referred users not Influencers so I must be doing something wrong.

回答1:

It is tough to figure out what is wrong without you mentioning if you are receiving errors, or failing to connect to Branch (log the tag "branch"), ect.

Here are some tips though for implementation. First, you should initialize in your Application class, not in onStart(). And if you have no Application class, then initialize in onCreate() only

public class YourApplication extends Application {
@Override
    public void onCreate() {

        Branch.getAutoInstance(this); // instantiate branch

    }
}

Depending upon what you are using, lets say you are using referral codes you need an identity which I see you set. This is done referencing a Branch object.

Branch branch = Branch.getInstance(context);

// set identity
branch.setIdentity("your user id");

After this, you can begin retrieving information such as receiving a referral code

branch.getReferralCode(null, defaultRefereeReward, null, Branch.REFERRAL_BUCKET_DEFAULT,
                Branch.REFERRAL_CODE_AWARD_UNLIMITED, Branch.REFERRAL_CODE_LOCATION_BOTH,
                new Branch.BranchReferralInitListener() {

                    @Override
                    public void onInitFinished(JSONObject jsonObject, BranchError branchError) {
                        if (FrameworkUtils.checkIfNull(branchError)) {
                            try {
                                // get and set referral code for current user
                                String currentCode = jsonObject.getString("referral_code");

                                // you can store the code in a model class if want   
                                ReferralInfoModel.setCurrentReferralCode(currentCode);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            Logger.e(TAG, branchError.getMessage());
                        }
                    }
                });

There are other methods you can use to track history such as

Branch.getCreditHistory()

I think the biggest reason things may not be working for you is that you do need to make your requests asynchronously. Use AsynTask. Hit the Branch url that you have.

For more examples refer to other posts: How to generate referral code using Branch.io Metrics?

And the documentation: https://github.com/BranchMetrics/Branch-Android-SDK#register-an-activity-for-direct-deep-linking-optional-but-recommended

And you can contact Branch directly to confirm your implementation. Cheers!