React-native android 无法初始化 Branch.io SDK

React-native android Trouble initializing Branch.io SDK

我在初始化 react-native-branch 5.0.0 SDK 时遇到问题。在 App 主组件的 useEffect 中调用 Branch.subscribe() 后:

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

控制台记录以下错误

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

我已按照文档中的步骤安装和配置 SDK,但我不知道自己哪里出错了。

这是我的 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>

在 MainActivity 中我有:

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";
    }

}

和 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);
  }

...

}


我尝试在仪表板中重置 branch_key,但结果是一样的。

首先,您的清单中缺少“应用程序”标签。其次分支键应该在“activity”标签之外。请按照下面给出的说明进行操作 url,您的应用程序将正常运行。

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

下面是您的清单应该是什么样子:-

<?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>