Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Branch.io returns " Session initialization already happened" error message

I checked this issue reported on git, many people reported it, but there was no proper solution available there.

It all works fine, if i app is killed and no process of app exists in memory.

But my use case is to get branch link from Push Notification.

I have also set splashActivity as SingleTask

android:launchMode="singleTask"

I am getting following error message.

BRANCH_SDK: Warning. Session initialization already happened. To force a new session, set intent extra, "branch_force_new_session", to true.

Following is my code, it checks if its there any data from branch, and pass the details to HomeActivity, HomeActivity then loads specific news item based on the id.

I don't know where to pass branch_force_new_session this info. If i pass it from Splash Intent, it just does not work.

I am using branchSDK 5.0.1

class SplashActivity : AppCompatActivity(), , Branch.BranchReferralInitListener{


    override fun onStart() {
        super.onStart()
        Branch.sessionBuilder(this).withCallback(this).withData(if (intent != null) intent.data else null).init()

        Log.d("BRANCH_SDK_00_", "onStart")
    }


 override fun onInitFinished(referringParams: JSONObject?, error: BranchError?) {

        if (error == null) {

            if(referringParams?.has("news") ==true){
                isNewsItemReceivedFromBranch = true
                branchAggregatedFeedItemId = referringParams.getInt("news")


                //StartHomeActivity()
            }

            initAppStartupProcesses()

        } else {
            Log.e("BRANCH_SDK", error.message)
            initAppStartupProcesses()
        }

    }


}

Manifest

<activity
            android:name=".activities.SplashActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>



            <!-- Branch URI Scheme -->
            <intent-filter>
                <data android:scheme="aaa" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
            <!-- Branch URI Scheme -->

            <!-- Branch App Links (optional) -->
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https" android:host="aaa.aa.aa" />
                <data android:scheme="https" android:host="aaa.aaaa" />
            </intent-filter>

</activity>
like image 362
dev90 Avatar asked Apr 14 '20 18:04

dev90


3 Answers

A Branchster here -

This happens when app is already in the foreground and the user tries to open the app through the deeplink.

This can be avoided by updating their oNewIntent override method as below-

  @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    // if activity is in foreground (or in backstack but partially visible) launching the same
    // activity will skip onStart, handle this case with reInitSession
    private Branch.BranchUniversalReferralInitListener branchReferralInitListener = new Branch.BranchUniversalReferralInitListener() {
    @Override public void onInitFinished(@Nullable BranchUniversalObject branchUniversalObject, @Nullable LinkProperties linkProperties, @Nullable BranchError error) {
        // do something with branchUniversalObject/linkProperties..
    }
}

If that doesn't work you can also try to use Branch.getInstance().getLatestReferringParams() instead of reading the params received in Branch.init in the latest version of the SDK.

I'll keep this post updated with respect to future development of the SDK.

like image 102
Kartik Shandilya Avatar answered Nov 18 '22 14:11

Kartik Shandilya


If this happens when app is in foreground set "branch_force_new_session"

override fun onNewIntent(intent: Intent) {
   super.onNewIntent(intent)
   this.intent = intent
   intent?.putExtra("branch_force_new_session", true)
   Branch.sessionBuilder(this).withCallback(branchListener).reInit()
}
like image 33
Akila Wasala Avatar answered Nov 18 '22 14:11

Akila Wasala


The same happened to me and nothing seemed to help, then I saw that the Branch tutorial was quite confusing - you need the call Branch.getAutoInstance(this) on the Application's onCreate, not the Splash/other branch receiving activity.

like image 2
Didi Avatar answered Nov 18 '22 15:11

Didi