Search code examples
androidreact-nativeadsbranch.io

React-native android Trouble initializing Branch.io SDK


I'm having problems initializing the react-native-branch 5.0.0 SDK. After calling the Branch.subscribe() in the useEffect of the main App component :

  useEffect(() => {
    Branch.subscribe(({ error, response }) => {
      console.log(error, "error");
      console.log(response, "response");
    });
  }, []);

The following error gets console logged

Trouble initializing Branch. Branch API Error: Please enter your branch_key in your project's manifest file first

I've followed the steps in the documentation to install and configure the SDK and I have no idea what I'm getting wrong.

Here's my AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.****.*********">

<activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:theme="@style/Theme.App.SplashScreen" android:screenOrientation="portrait">
...
      <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="myappuri" android:host="open" />
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
      </intent-filter>

       <!-- Branch App Links -->
      <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="myactualappname.app.link" />
      </intent-filter>
      
      <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_*********"/>
      <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="key_test_*******"/>
      <meta-data android:name="io.branch.sdk.TestMode" android:value="false" />

...

    </activity>
</manifest>

In the MainActivity I have :

import io.branch.rnbranch.*;
import android.content.Intent;

...

public class MainActivity extends ReactActivity {
    // Override onStart, onNewIntent:
    @Override
    protected void onStart() {
        super.onStart();
        RNBranchModule.initSession(getIntent().getData(), this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
      if (intent != null &&
            intent.hasExtra("branch_force_new_session") && 
            intent.getBooleanExtra("branch_force_new_session",false)) {
          RNBranchModule.onNewIntent(intent);
      }
    }

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "main";
    }

}

And the MainApplication

...
import io.branch.rnbranch.RNBranchModule;

public class MainApplication extends Application implements ReactApplication {
...

 @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);

    initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
    ApplicationLifecycleDispatcher.onApplicationCreate(this);
    RNBranchModule.getAutoInstance(this);
  }

...

}


I tried resetting the branch_key in the Dashboard but the result is the same.


Solution

  • First of all "application" tag is missing from your manifest. Secondly branch keys should be outside of "activity" tag. Please follow instructions given in below url and your app will work just fine.

    https://help.branch.io/developers-hub/docs/android-basic-integration

    Below is how should your manifest look like:-

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.****.*********">
    
    <application
        android:allowBackup="true"
        android:name="com.eneff.branch.example.android.CustomApplicationClass"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    
        <!-- Launcher Activity to handle incoming Branch intents -->
        <activity
            android:name=".LauncherActivity"
            android:launchMode="singleTask"
            android:label="@string/app_name"
            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="yourapp" android:host="open" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
    
            <!-- Branch App Links -->
            <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="example.app.link" />
                <!-- example-alternate domain is required for App Links when the Journeys/Web SDK and Deepviews are used inside your website.  -->
                <data android:scheme="https" android:host="example-alternate.app.link" />
            </intent-filter>
        </activity>
    
        <!-- Branch init -->
        <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_*********"/>
      <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="key_test_*******"/>
      <meta-data android:name="io.branch.sdk.TestMode" android:value="false" />
    
    </application>