11

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>
dev90
  • 7,187
  • 15
  • 80
  • 153
  • 1
    Please share your Manifest.xml file as well. Also does the issue persist if you use `Branch.getInstance().getLatestReferringParams()` instead of reading the params from init callback – Kartik Shandilya Apr 15 '20 at 06:58
  • @NovoLucas : I have added Manifest file. please check. Regarding `Branch.getInstance().getLatestReferringParams()`, Do i need to check this before `Branch.sessionBuilder` ? – dev90 Apr 15 '20 at 08:22
  • This should come after the sessionBuilder – Kartik Shandilya Apr 16 '20 at 03:59

4 Answers4

5

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.

Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
3

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()
}
ajw
  • 2,568
  • 23
  • 27
2

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.

Didi
  • 350
  • 1
  • 4
  • 10
2

If you want to force a new session you can do this on the intent before initializing the Branch session. (This works when I test on SDK version 5.3.0).

override fun onStart() {
    super.onStart()

    //Add this line before the Branch.sessionBuilder() or Branch.getInstance().initSession() method.
    intent.putExtra("branch_force_new_session", true)

    Branch.sessionBuilder(this).withCallback { branchUniversalObject, linkProperties, error ->
        val metadata = branchUniversalObject?.contentMetadata?.customMetadata
        val deeplink = linkProperties?.controlParams?.get("\$deeplink_path")

        //Handle the deeplink here
    }.withData(intent.data).init()
}
Markymark
  • 2,804
  • 1
  • 32
  • 37