Search code examples
react-nativebranch.io

Branch io link +clicked_branch_link is always false


I'm trying to implement Branch io links to my android app but I cant get them to work correctly. Using this guide: https://help.branch.io/developers-hub/docs/react-native

Dashboard configuration: Android URI Scheme

https://com.example.host://

Android App links are enabled, app is selected from the play store and I have the SHA256 Cert Fingerprints entered.

Android manifest

        <!-- Branch URI Scheme -->
        <intent-filter>
            <data android:scheme="example.scheme" 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="http" android:host="com.example.host"/>
            <data android:scheme="https" android:host="com.example.host"/>
        </intent-filter>

    <!-- Branch keys -->
        <!-- (Omit if setting keys in branch.json) -->
        <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_mykey"/>
        <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="key_test_mykey"/>

MainActivity

package com.efri_mobile;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.zoontek.rnbootsplash.RNBootSplash;
import io.branch.rnbranch.*;
import android.content.Intent;


public class MainActivity extends ReactActivity {

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

  @Override
  protected void onStart() {
      super.onStart();
      RNBranchModule.initSession(getIntent().getData(), this);
  }

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

  @Override
  protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {

      @Override
      protected void loadApp(String appKey) {
        RNBootSplash.init(MainActivity.this); // <- initialize the splash screen
        super.loadApp(appKey);
      }
    };
  }
}

MainApplication

package com.efri_mobile;

import android.app.Application;
import android.content.Context;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.oblador.vectoricons.VectorIconsPackage;
import com.oblador.vectoricons.VectorIconsPackage;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import io.branch.rnbranch.RNBranchModule;


public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost =
      new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
          return packages;
        }

        @Override
        protected String getJSMainModuleName() {
          return "index";
        }
      };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    RNBranchModule.getAutoInstance(this);
    SoLoader.init(this, /* native exopackage */ false);
    initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  }

  /**
   * Loads Flipper in React Native templates. Call this in the onCreate method with something like
   * initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
   *
   * @param context
   * @param reactInstanceManager
   */
  private static void initializeFlipper(
      Context context, ReactInstanceManager reactInstanceManager) {
    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.efri_mobile.ReactNativeFlipper");
        aClass
            .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
            .invoke(null, context, reactInstanceManager);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
}

App.tsx

//Read branch link
  useEffect(() => {
    const unsubscribe = branch.subscribe(({ error, params, uri }) => {
      if (error) {
        console.error('Error from Branch: ' + error);
        return;
      }
      console.log('PARAMS: ', params, uri);
    });

    return () => unsubscribe();
  });

I am opening the link (example.app.link) from my dashboard app by clicking on a button. App opens correctly but +clicked_branch_link is always false.

I'm Not using the test link. The live key is correctly set in my android manifest.

This is my third attempt at implementing branch links in a year, and I always had problems with this part. What am I missing? Do I need to have Branch URI scheme if I want to use App links?


Solution

  • After a long time, I finally found a solution that somewhat works. The branch guide is quite misleading and following it doesn't work at all.

    I followed this guide: https://dev.to/chakrihacker/integrating-branch-in-react-native-0-60-3lfj

    If you follow the main branch guide, make sure you change parts of that to match the ones below:

    info.plist use the following for adding keys and URL schemas

        <key>branch_key</key>
        <string>key_live_******</string>
        <key>branch_app_domain</key>
        <string>9jzhz.app.link</string>
        <key>CFBundleURLTypes</key>
        <array>
            <dict>
                <key>CFBundleURLSchemes</key>
                <array>
                    <string>branchapp</string>
                </array>
            </dict>
        </array>
    

    AppDelegate There are some different things here and in the main guide

    #import <RNBranch/RNBranch.h>
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {   
        //  Branch
        [RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];
    
        RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
        RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                                                                        moduleName:@"BranchExample"
                                                                                            initialProperties:nil];
    
        rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
    
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        UIViewController *rootViewController = [UIViewController new];
        rootViewController.view = rootView;
        self.window.rootViewController = rootViewController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
            if (![RNBranch.branch application:app openURL:url options:options]) {
                    // do other deep link routing for the Facebook SDK, Pinterest SDK, etc
            }
            return YES;
    }
    
    - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
            return [RNBranch continueUserActivity:userActivity];
    }
    

    Make sure you update the key, branch_app_domain and branchapp url scheme with your values

    And the most important thing: USE THE QUICK LINKS!

    Using the main link does not work! You must create the quick link in the branch dashboard, and use that instead! I got it to work for IOS and android.