Skip to content

Session initialization already happened. To force a new session, set intent extra, "branch_force_new_session" #1286

@doaa-abdelkarim

Description

@doaa-abdelkarim

Describe the bug

Deferred deep link does not work with me. I get "Session initialization already happened. To force a new session, set intent extra, "branch_force_new_session" after installing app from play store instead and the same happens If I opened app organically.

Here is my integration:

//My handler
@singleton
class BranchIOHandler
@Inject
constructor() {
private var isSessionInitialized = false

fun handleIntent(
    activity: Activity,
    intent: Intent,
    shouldForceNewSession: Boolean = false,
    onReferralReceived: (param1: String, param2: String) -> Unit,
    onFallback: () -> Unit = {}
) {
    val builder =
        Branch
            .sessionBuilder(activity)
            .withCallback { referringParams, error ->
                if (error != null) {
                    onFallback()
                    return@withCallback
                }

                val param1= referringParams?.getString("param1")
                val param2= referringParams?.getString("param2")
               
                if (!param1.isNullOrEmpty() && !param2.isNullOrEmpty()) {
                    onReferralReceived(param1, param2)
                } else {
                    onFallback()
                }
            }

    if (shouldForceNewSession) {
        // Branch reinit (in case Activity is already visible when Branch Link is clicked)
        // Will re-initialize only if `branch_force_new_session=true` intent extra is set
        intent.putExtra("branch_force_new_session", true)
        builder
            .reInit()
    } else {
        if (!isSessionInitialized) {
            isSessionInitialized = true
            builder
                .withData(intent.data)
                .init()
        }
    }
}

fun reset() {
    isSessionInitialized = false
}

}

//In gradle
implementation ‘io.branch.sdk.android:library:5.18.2’

//In Application class

override fun onCreate() {
super.onCreate()
// Branch logging for debugging
if (BuildConfig.DEBUG) {
Branch.enableLogging()
}

// Branch object initialization
Branch.getAutoInstance(this)

}


//In MainActivity
override fun onStart() {
super.onStart()

branchIOHandler.handleIntent(
    activity = this,
    intent = intent,
    shouldForceNewSession = false,
    onReferralReceived = { param1, param2->},
    onFallback = {}
)

}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)

this.intent = intent

    intent?.data?.let {
    branchIOHandler.handleIntent(
        activity = this,
        intent = intent,
        shouldForceNewSession = true,
        onReferralReceived = { param1, param2-> },
        onFallback = {}
    )
}

}

Steps to reproduce

1.Click the deep link
2.If the app is not installed, it navigates me to the play store
3.After installing and opening the app, I get this error
4.The same happens If I opened app organically

Expected behavior

I expect to retrieve the link data after installing the app and not see this warning if I open the app normally.

SDK Version

5.18.2

Make and Model

Real device: Samsung, and emulator

OS

14 and 16

Additional Information/Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions