如何使用Branch.io实现引用系统

时间:2016-08-27 13:29:46

标签: java android branch branch.io referrals

我已按照文件说明,我不知道有什么不对,我真的很感激有些见解。

清单:

<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>

活动

@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);
}

邀请时的片段:

    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+"");
        }
    });

我在Branch Dashboard设置上处理了每个平台的链接。

发生的事情是,当我将此链接发送给某人时,它按预期打开Play商店并可以下载,但我没有获得积分,但它没有显示任何被推荐的用户而不是影响者,所以我一定做错了。

enter image description here

1 个答案:

答案 0 :(得分:1)

如果你没有提到是否收到错误,或者没有连接到分支(记录标签“分支”)等,很难弄清楚出了什么问题。

以下是实施的一些提示。首先,您应该在Application类中初始化,而不是在onStart()中初始化。如果您没有Application类,则仅在onCreate()中初始化

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

        Branch.getAutoInstance(this); // instantiate branch

    }
}

根据您使用的内容,假设您使用的是引荐代码,您需要我看到您设置的身份。这是在引用Branch对象时完成的。

Branch branch = Branch.getInstance(context);

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

在此之后,您可以开始检索信息,例如接收推荐代码

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());
                        }
                    }
                });

您可以使用其他方法跟踪历史记录,例如

Branch.getCreditHistory()

我认为事情可能不适合你的最大原因是你确实需要异步提出请求。使用AsynTask。点击你拥有的分行网址。

有关更多示例,请参阅其他帖子:How to generate referral code using Branch.io Metrics?

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

您可以直接与Branch联系以确认您的实施。干杯!

相关问题