Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Branch.io initSession returns empty referringParams json

Preconditions 1. App starts with LinkActivity, at this point we have no deep link intent, it's ok.

  1. Main activity launched. There we are able to click the deep link.
  2. By clicking on deep link opens LinkActivity, uri is correct, referringParams json is not empty (ok). But...
  3. When we replaying step 2: uri is correct, but the reffering params are empty: "{}"; All other tries are with the same result.

Only when we pausing the app (for example switching to the recent apps menu) and then returning to the app - deep link works as expected, but only at first try. May be some issues with the session close (but in the current version of the sdk it self controls session close)

public class LinkActivity extends AppCompatActivity {

        private static final String TAG = LinkActivity.class.getSimpleName();

        @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);
        }

        @Override
        protected void onStart() {
            super.onStart();

            Uri uri = getIntent().getData();

            Log.w(TAG, "uri: " + uri);

            Branch.getInstance().initSession(new Branch.BranchReferralInitListener() {
                @Override
                public void onInitFinished(JSONObject referringParams, BranchError error) {
                    Log.w(TAG, "json: " + referringParams);
                    startActivity(new Intent(LinkActivity.this, MainActivity.class));
                }
            }, uri, this);
        }
    }

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }

    public class BranchApplication extends Application {

        @Override
        public void onCreate() {
            super.onCreate();
            Branch.enableLogging();
            Branch.getAutoInstance(this);
        }
    }

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.test.myapp">

        <uses-permission android:name="android.permission.INTERNET"/>

        <application
            android:name=".BranchApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

            <activity android:name=".LinkActivity">

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>

                <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:host="myapp.link"
                        android:scheme="https" />
                </intent-filter>
            </activity>

            <activity android:name=".MainActivity"/>

            <meta-data
                android:name="io.branch.sdk.BranchKey"
                android:value="@string/branch_io_live_key" />

            <meta-data
                android:name="io.branch.sdk.BranchKey.test"
                android:value="@string/branch_io_test_key" />

            <meta-data
                android:name="io.branch.sdk.TestMode"
                android:value="false" />
        </application>
    </manifest>

implementation "io.branch.sdk.android:library:2.14.3"

Update: Even with android:launchMode="singleInstance" for LinkActivity steel reproduces (I don't think this is the case).

Udpate2: Bhardwaj mentioned that no need to call initSession when we initing Branch via getAutoInstance. But how to get refferingParams from uri in that case?

Update3: From the Branch.checkIntentForSessionRestart doc:

Check for forced session restart. The Branch session is restarted if the incoming intent has branch_force_new_session set to true. This is for supporting opening a deep link path while app is already running in the foreground. Such as clicking push notification while app in foreground.

So, My desired behavior is matches this description. But how to force session restart?

like image 455
Sinigami Avatar asked Jan 18 '18 15:01

Sinigami


1 Answers

 if(!initiatedBranchDeepLinks) {
        // Configure Branch.io
        initiatedBranchDeepLinks = true;
        Branch branch = Branch.getInstance();
        branch.initSession(new Branch.BranchReferralInitListener(){
            @Override
            public void onInitFinished(JSONObject referringParams, BranchError error) {
                if (error == null) {
                    // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
                    // params will be empty if no data found
                    // ... insert custom logic here ...
                    String message = "Branch.io onInitFinished. Params: " + referringParams.toString();
                    Log.d(TAG, message);

                } else {
                    Log.i(TAG, error.getMessage());
                }
            }
        }, this.getIntent().getData(), this);
    }

Here is Branch Test Bed app: https://github.com/BranchMetrics/android-branch-deep-linking/tree/master/Branch-SDK-TestBed

You can use this as a reference and see what you are doing incorrectly.

like image 55
PRATEEK BHARDWAJ Avatar answered Nov 17 '22 12:11

PRATEEK BHARDWAJ