GT2/GT2-Android/node_modules/react-native-branch/docs/setup.md

4.8 KiB

Setting Up Branch

After installing Branch, you will need to set up your android and ios apps to handle incoming links:

Note: When using react-native < 0.40 and react-native-branch 0.9, specify imports without a path prefix, e.g. #import "RNBranch.h" instead of #import <react-native-branch/RNBranch.h>.

iOS project

  1. Modify AppDelegate.m as follows:

    #import <react-native-branch/RNBranch.h> // at the top
    
    // Initialize the Branch Session at the top of existing didFinishLaunchingWithOptions
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      // Uncomment this line to use the test key instead of the live one.
      // [RNBranch useTestInstance]
      [RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES]; // <-- add this
    
      NSURL *jsCodeLocation;
      //...
    }
    
    // Add the openURL and continueUserActivity functions
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
      if (![RNBranch handleDeepLink:url]) {
        // 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];
    }
    
  2. Add a String entry branch_key with your Branch key to your info.plist

  3. Register a URI Scheme for Direct Deep Linking (optional but recommended)

  4. Configure for Universal Linking

android project

  1. Add RNBranchPackage to packages list in MainApplication.java (android/app/src/[...]/MainApplication.java)

    // ...
    
    // import Branch and RNBranch
    import io.branch.rnbranch.*;
    import io.branch.referral.Branch;
    
    //...
    
    // add RNBranchPackage to react-native package list
    @Override
      protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
                new MainReactPackage(),
                new RNBranchPackage(), // <-- add this
    
    // ...
    
    // add onCreate() override
    @Override
    public void onCreate() {
      super.onCreate();
      Branch.getAutoInstance(this);
    }
    
  2. Override onStart and onNewIntent in MainActivity.java to handle Branch links (android/app/src/[...]/MainActivity.java)

    import io.branch.rnbranch.*; // <-- add this
    import android.content.Intent; // <-- and this
    
    public class MainActivity extends ReactActivity {
    
        @Override
        protected String getMainComponentName() {
            return "base";
        }
    
        // Override onStart, onNewIntent:
        @Override
        protected void onStart() {
            super.onStart();
            RNBranchModule.initSession(this.getIntent().getData(), this);
        }
    
        @Override
        public void onNewIntent(Intent intent) {
            this.setIntent(intent);
        }
        // ...
    }
    
  3. Configure AndroidManifest.xml. Be sure to set android:launchMode="singleTask" on your main activity.

  4. Register for Google Play Install Referrer. The "receiver" element needs to be added to the "application" node in AndroidManifest.xml

  5. Register a URI scheme

  • The "intent-filter" element needs to be added to the activity node, whose android:name is "com.yourAppName.MainActivity". This node is in the "application" node.
  • If you already have an intent-filter tag, this has to be added as an additional one.
  • Make sure to replace "yourApp" with the scheme you specified in the Branch dashboard.
  1. Enable Auto Session Management. Simply add the "android:name" attribute to your "application" node in your AndroidManifest.xml

  2. Enable App Links for Android M and above (optional but recommended)

  3. Add your Branch key to AndroidManifest: Inside of application node add <meta-data android:name="io.branch.sdk.BranchKey" android:value="your_branch_key" />