Windows C-Sharp Basic Integration

Configure Branch

Adding the Branch SDK to a Windows Solution

Adding the Json Libary

First add Newtonsoft.Json from NuGet:

  1. Right-click on project in solution explorer and click on Manage NuGet Packages...
  2. Click on Browse tab, find Newtonsoft.Jsonand install it

Adding the Branch SDK

  1. Clone this repository to the local machine: https://github.com/BranchMetrics/windows-branch-deep-linking.git
  2. Add the branch_debug_0.1.0 or branch_0.1.0 library to project from DLLs folder

Integrate the Branch SDK

I. Create project in visual studio

  • Open Visual Studio and create a new Blank App (Windows Universal) project

II. Register app to handle deep link

  • Select Package.appxmanifest in Solution Explorer and press F7

  • Add the Windows.appUriHandler extension with your link:

        <Applications>
            <Application>
                <Extensions>
                    <uap3:Extension Category="windows.appUriHandler">
                        <uap3:AppUriHandler>
                            <uap3:Host Name="testbed-windows.app.link" />
                        </uap3:AppUriHandler>
                    </uap3:Extension>
                </Extensions>
            </Application>
        </Applications>
  • Add DeepLinkHandler.cs in your project and write in it notification system that the application was activated by deep link for MainPage.xaml.cs, for example:
        public static class DeepLinkHandler {
            public static event Action<string> OnAppDeepLinkEvent = delegate { };

            public static void OnAppDeepLink(string url) {
                OnAppDeepLinkEvent.Invoke(url);
            }
        }
  • Navigate to App.xaml.cs in your app Visual Studio solution and in OnActivated() add handling for linked content:
        protected override void OnActivated(IActivatedEventArgs e) {
            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null) {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();
            rootFrame.NavigationFailed += OnNavigationFailed;

            Window.Current.Content = rootFrame;
            }

            Type deepLinkPageType = typeof(MainPage);
            if (e.Kind == ActivationKind.Protocol) {
            var protocolArgs = (ProtocolActivatedEventArgs)e;
            DeepLinkHandler.OnAppDeepLink(protocolArgs.Uri.AbsoluteUri);
            }

            if (rootFrame.Content == null) {
            // Default navigation
            rootFrame.Navigate(deepLinkPageType, e);
            }

            // Ensure the current window is active
            Window.Current.Activate();
        }
  • Subscribe to OnAppDeepLink event in MainPage.xaml.cs and in callback handler call InitSession with uri
  • Change OnSuspending method in App.xaml.cs
        private async void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            await LibraryAdapter.GetPrefHelper().SaveAll();
            deferral.Complete();
        }

III. Configuring

  • In Solution Explorer in folder Assets create folder Configs and in it folder create two text files (.txt) with names live_branch_key.txt and test_branch_key.txt and fill they with branch keys from your app dashboard
  • You may configure your app with that methods:
    • Branch.I.SetDebug(bool isDebug); - If isDebug is true then app will use Test Branch key if specified. Otherwise, the app will use the Live Branch key.
    • Branch.I.SetNetworkTimeout(int timeout); - That method is set timeout for all requests
    • Branch.I.SetMaxRetries(int maxRetries); - That method is set max retries for failed requests
    • Branch.I.SetRetryInterval(int retryInterval); - That method is set retry interval between request retries
    • Branch.I.SetRequestMetadata(string key, string value) - That method is set common metadata for all requests
    • Branch.I.DisableTracking(bool disableTracking); - This method determines whether it is possible to use user data (Many functions will not be available if tracking is disabled)

Example Code

To help you get started, we have provided this example for learning how to to write Branch UWP projects - UWP Example

Configuring a Win32 App

Adding the Branch SDK

  1. Clone this repository to the local machine: https://github.com/BranchMetrics/branch-windows-sdk.git

  2. Add the branch_win32_debug_0.1.0 or branch_win32_0.1.0 libary to project from DLLs folder

I. Create project in visual studio

  • Open Visual Studio and create a new Console App (.NET Framework) project

II. Configuring

  • In Output build create folder Configs and in it folder create two text files (.txt) with names live_branch_key.txt and test_branch_key.txt and fill they with branch keys from your app dashboard
  • You may configure your app with that methods:
    • Branch.I.SetDebug(bool isDebug); - If isDebug is true then app will use Test Branch key if specified. Otherwise, the app will use the Live Branch key.
    • Branch.I.SetNetworkTimeout(int timeout); - That method is set timeout for all requests
    • Branch.I.SetMaxRetries(int maxRetries); - That method is set max retries for failed requests
    • Branch.I.SetRetryInterval(int retryInterval); - That method is set retry interval between request retries
    • Branch.I.SetRequestMetadata(string key, string value) - That method is set common metadata for all requests
    • Branch.I.DisableTracking(bool disableTracking); - This method determines whether it is possible to use user data (Many functions will not be available if tracking is disabled)

Example code

To help you get started, we have provided this example for learning how to to write Branch UWP projects - Win32 Example

Updated 3 months ago

Windows C-Sharp Basic Integration


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.