You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Branch.io initSession返回空referringParams问题咨询

I’ve worked through your issue with the Branch SDK returning empty referringParams after the first deep link click, and let’s walk through exactly what’s going wrong and how to fix it.

What’s Causing the Problem?

The core issue ties to how Branch manages sessions and how you’re handling incoming intents. When you repeatedly trigger a deep link without pausing the app, Branch doesn’t automatically reset the session—so it returns an empty {} instead of fresh referral data. Even with singleInstance launch mode, your current code isn’t properly reinitializing the session when new intents come in.

Step-by-Step Fixes

Right now, you’re just updating the intent in onNewIntent but not reinitializing the Branch session. When your LinkActivity is already running, new deep link intents go to onNewIntent, not onStart—so you need to trigger a fresh session init here. Update your LinkActivity:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // Mark this intent as the current one and force a new session
    intent.putExtra("branch_force_new_session", true);
    setIntent(intent);

    Uri uri = intent.getData();
    Branch.getInstance().initSession(new Branch.BranchReferralInitListener() {
        @Override
        public void onInitFinished(JSONObject referringParams, BranchError error) {
            if (error == null) {
                Log.w(TAG, "Fresh deep link params: " + referringParams);
                startActivity(new Intent(LinkActivity.this, MainActivity.class));
            } else {
                Log.e(TAG, "Branch session error: " + error.getMessage());
            }
        }
    }, uri, this);
}

2. Avoid Redundant Session Initialization in onStart

You don’t want to reinitialize the session every time onStart is called (like when returning from MainActivity). Add a check to only run the initial session setup when the activity is first launched:

@Override
protected void onStart() {
    super.onStart();
    // Skip if this is a resume from history (not a fresh launch/deep link)
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
        return;
    }

    Uri uri = getIntent().getData();
    Log.w(TAG, "Initial launch uri: " + uri);
    Branch.getInstance().initSession(new Branch.BranchReferralInitListener() {
        @Override
        public void onInitFinished(JSONObject referringParams, BranchError error) {
            Log.w(TAG, "Initial referral params: " + referringParams);
            startActivity(new Intent(LinkActivity.this, MainActivity.class));
        }
    }, uri, this);
}

3. Use singleTask Launch Mode (Instead of singleInstance)

singleInstance is overly restrictive for most deep link scenarios. Switch to singleTask in your manifest—it ensures only one instance of LinkActivity exists, and new intents are routed to onNewIntent correctly:

<activity android:name=".LinkActivity"
          android:launchMode="singleTask">
    <!-- Your MAIN and VIEW intent filters here -->
</activity>

4. Understand getAutoInstance vs initSession

When you use Branch.getAutoInstance() in your Application class, Branch handles basic session setup on app launch. But for deep links, you still need to explicitly call initSession with the incoming intent—this is how Branch parses the deep link data and returns the referringParams.

Quick Recap

  • Always update the intent and reinitialize the Branch session in onNewIntent for existing activities.
  • Use branch_force_new_session to tell Branch to treat each deep link as a fresh session.
  • Restrict initial session setup in onStart to only first-time launches.

内容的提问来源于stack exchange,提问作者Sinigami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:35:29