React-native-branch-deep-linking-attribution: How to use with react native 0.60+ and wix react native navigation 3+ on Android

Created on 12 Sep 2019  路  4Comments  路  Source: BranchMetrics/react-native-branch-deep-linking-attribution

I haven't found any examples for MainActivity.java and MainApplication.java when both wix react native navigation and react-native-branch are used in the same project. If I missed any docs please point me. If anyone has working MainActivity.java and MainApplication.java for react native 0.60+, wix react native navigation and react native branch please share.

Most helpful comment

Hey there hope this helps:

/* MainActivity.java */
package com.package.name;
import com.reactnativenavigation.NavigationActivity;
import io.branch.rnbranch.*; // <-- add this
import android.content.Intent; // <-- and this
import android.os.Bundle;

public class MainActivity extends NavigationActivity {
    @Override
    public void onStart() {
        super.onStart();
        RNBranchModule.initSession(getIntent().getData(), this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
}

// MainApplication.java
package com.package.name;

import com.facebook.react.PackageList;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import androidx.multidex.MultiDex;
import android.content.Context;
import androidx.annotation.Nullable;

/* Packages */
import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.NavigationReactNativeHost;
import com.reactnativenavigation.react.ReactGateway;
import com.microsoft.codepush.react.CodePush;
import io.branch.rnbranch.RNBranchModule;


public class MainApplication extends NavigationApplication {
    @Override
    protected ReactGateway createReactGateway() {
        ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
            @Override
            protected String getJSMainModuleName() {
                return "index";
            }

            @Nullable
            @Override
            protected String getJSBundleFile() {
                return CodePush.getJSBundleFile();
            }
        };
        return new ReactGateway(this, isDebug(), host);
    }

    @Override
    public boolean isDebug() {
          return BuildConfig.DEBUG;
    }

    @Nullable
    @Override
    public List<ReactPackage> createAdditionalReactPackages() {
        @SuppressWarnings("UnnecessaryLocalVariable")
        List<ReactPackage> packages = new PackageList(this).getPackages();
        return packages;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initializeFlipper(this);
        RNBranchModule.getAutoInstance(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
      /**
   * Loads Flipper in React Native templates.
   *
   * @param context
   */

  private static void initializeFlipper(Context context) {
    if (BuildConfig.DEBUG) {
      try {
        /*
         We use reflection here to pick up the class that initializes Flipper,
        since Flipper library is not available in release mode
        */
        Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
        aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
}

All 4 comments

Hey there hope this helps:

/* MainActivity.java */
package com.package.name;
import com.reactnativenavigation.NavigationActivity;
import io.branch.rnbranch.*; // <-- add this
import android.content.Intent; // <-- and this
import android.os.Bundle;

public class MainActivity extends NavigationActivity {
    @Override
    public void onStart() {
        super.onStart();
        RNBranchModule.initSession(getIntent().getData(), this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
}

// MainApplication.java
package com.package.name;

import com.facebook.react.PackageList;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import androidx.multidex.MultiDex;
import android.content.Context;
import androidx.annotation.Nullable;

/* Packages */
import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.NavigationReactNativeHost;
import com.reactnativenavigation.react.ReactGateway;
import com.microsoft.codepush.react.CodePush;
import io.branch.rnbranch.RNBranchModule;


public class MainApplication extends NavigationApplication {
    @Override
    protected ReactGateway createReactGateway() {
        ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
            @Override
            protected String getJSMainModuleName() {
                return "index";
            }

            @Nullable
            @Override
            protected String getJSBundleFile() {
                return CodePush.getJSBundleFile();
            }
        };
        return new ReactGateway(this, isDebug(), host);
    }

    @Override
    public boolean isDebug() {
          return BuildConfig.DEBUG;
    }

    @Nullable
    @Override
    public List<ReactPackage> createAdditionalReactPackages() {
        @SuppressWarnings("UnnecessaryLocalVariable")
        List<ReactPackage> packages = new PackageList(this).getPackages();
        return packages;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initializeFlipper(this);
        RNBranchModule.getAutoInstance(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
      /**
   * Loads Flipper in React Native templates.
   *
   * @param context
   */

  private static void initializeFlipper(Context context) {
    if (BuildConfig.DEBUG) {
      try {
        /*
         We use reflection here to pick up the class that initializes Flipper,
        since Flipper library is not available in release mode
        */
        Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
        aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
}

@jinshin1013, thank you for your help! Although it's not relevant for me anymore because I already switched to another solution. Since I won't check if it works I presume it does and am closing the issue.

thank you for your help! Although it's not relevant for me anymore because I already switched to another solution. Since I won't check if it works I presume it does and am closing the issue.

Hello Mr terred, How did you fix this, So please help me?

I just updated to Branch 5.0.1 and trying to update my android files. I'm running into issues with onStart and getMainComponentName not being possible to override. Do you have an example of how to fix this when using react native navigation?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Amurmurmur picture Amurmurmur  路  6Comments

thaitanloi365 picture thaitanloi365  路  6Comments

alaminopu picture alaminopu  路  5Comments

stefanlenoach picture stefanlenoach  路  8Comments

felixchan picture felixchan  路  5Comments