Skip to content

stevecarlton/capacitor-branch-deep-links

 
 

Repository files navigation

capacitor-branch-deep-links

Capacitor plugin for branch.io deep links.

Bound State Software

npm install capacitor-branch-deep-links

Usage

+ import { Plugins } from '@capacitor/core';
+ import { BranchInitEvent } from 'capacitor-branch-deep-links';

+ const { BranchDeepLinks } = Plugins;

  @Component({
    selector: 'app-root',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.scss']
  })
  export class AppComponent {
    constructor(
      private platform: Platform,
      private splashScreen: SplashScreen,
      private statusBar: StatusBar
    ) {
      this.initializeApp();
    }

    initializeApp() {
      this.platform.ready().then(() => {
        this.statusBar.styleDefault();
        this.splashScreen.hide();
+       BranchDeepLinks.addListener('init', (event: BranchInitEvent) => {
+         // Retrieve deeplink keys from 'referringParams' and evaluate the values to determine where to route the user
+         // Check '+clicked_branch_link' before deciding whether to use your Branch routing logic
+         console.log(event.referringParams);
+       });

+       BranchDeepLinks.addListener('initError', (error: any) => {
+         console.error(error);
+       });
      });
    }
  }

Android setup

Follow the Branch docs to:

  1. Configure Branch

If your app is in the Google Play Store, update build.grade with the necessary dependencies:

  dependencies {
+   implementation 'com.android.installreferrer:installreferrer:1.1'
+   implementation 'com.google.android.gms:play-services-appindexing:9.+' // App indexing
+   implementation 'com.google.android.gms:play-services-ads:9+' // GAID matching
  }

Update src/main/res/values/strings.xml with your configuration:

  <?xml version='1.0' encoding='utf-8'?>
  <resources>
      <string name="app_name">Ionic Starter</string>
      <string name="title_activity_main">Ionic Starter</string>
      <string name="package_name">io.ionic.starter</string>
      <string name="fileprovider_authority">io.ionic.starter.fileprovider</string>
+     <string name="custom_url_scheme">io.ionic.starter</string>
+     <string name="branch_key">key_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</string>
+     <string name="branch_test_key">key_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</string>
  </resources>

Register the plugin in your Activity:

+ import android.content.Intent;
+ import co.boundstate.BranchDeepLinks;

  public class MainActivity extends BridgeActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      // Initializes the Bridge
      this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
        // Additional plugins you've installed go here
        // Ex: add(TotallyAwesomePlugin.class);
+       add(BranchDeepLinks.class);
      }});
    }

+   @Override
+   protected void onNewIntent(Intent intent) {
+     this.setIntent(intent);
+     super.onNewIntent(intent);
+   }
  }

Declare BranchApp as your application class in src/main/AndroidManifest.xml:

  <application
+     android:name="io.branch.referral.BranchApp"

Provide your Branch config within <application>:

<meta-data android:name="io.branch.sdk.BranchKey" android:value="@string/branch_key" />
<meta-data android:name="io.branch.sdk.BranchKey.test" android:value="@string/branch_test_key" />
<meta-data android:name="io.branch.sdk.TestMode" android:value="false" /> <!-- Set to true to use test key -->

Add your Branch App Links (optional) in a new <intent-filter> within <activity>:

<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="xxxx.app.link" />
    <data android:scheme="https" android:host="xxxx-alternate.app.link" />
</intent-filter>

Test that it works!

iOS setup

Follow the Branch docs to:

  1. Configure Branch
  2. Configure bundle identifier
  3. Configure associated domains
  4. Configure entitlements
  5. Configure Info.plist

You can use the Branch wizard to walk you through the process
(skip the Get the SDK files and Start a Branch session steps)

Update the project:

npx cap update ios

Make the following changes to your AppDelegate.swift file:

+ import Branch

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
+   Branch.setUseTestBranchKey(true) // if you are using the TEST key
+   Branch.getInstance().initSession(launchOptions: launchOptions)
    return true
  }

  func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // Called when the app was launched with a url. Feel free to add additional processing here,
    // but if you want the App API to support tracking app url opens, make sure to keep this call
+   Branch.getInstance().application(app, open: url, options: options)
    return CAPBridge.handleOpenUrl(url, options)
  }

  func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    // Called when the app was launched with an activity, including Universal Links.
    // Feel free to add additional processing here, but if you want the App API to support
    // tracking app url opens, make sure to keep this call
+   Branch.getInstance().continue(userActivity)
    return CAPBridge.handleContinueActivity(userActivity, restorationHandler)
  }

+ func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
+   Branch.getInstance().handlePushNotification(userInfo)
+ }

Test that it works!

About

Capacitor plugin for branch.io deep links

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 31.5%
  • JavaScript 27.7%
  • Java 27.2%
  • TypeScript 10.1%
  • Objective-C 1.9%
  • Ruby 1.6%