In-App Routing

概述

When a Branch link is opened, either your app launches or users are taken to the App/Play store to download it. Deep links improve this process by routing users directly to specific content after your app launches. With Branch, this works even if users have to stop and download the app first (a.k.a., "deferred deep links"). Deep links are an incredibly important part of delivering a high-quality user experience.

我们将引导您完成3个配置选项:

  • 选项1:在路由回调中建立自定义路由—在打开应用后立即进行路由
  • 选项 2:让 Branch 使用您现有的深度链接路由
  • 选项3:使用 Branch 的简单配置深度链接路由—自动路由

🚧

iOS Deferred Deep Linking

With the introduction of iOS 15 and iCloud+ Private Relay, Branch released support for deferred deep linking using NativeLink™

To implement iOS NativeLink™, follow the instructions in our developer documentation.

选项1:在路由回调中构建自定义路由

在应用打开时立即进行路由

Inside the deep link handler callback that you register in initSession, you will want to examine the parameters dictionary to determine whether the user opened a Branch link. Below is an example assuming that the links correspond to pictures. Below are some examples from iOS and Android where we're using the pictureId key to route, but you can see more code snippets for the other platforms here.

iOS

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

      // initialize the session, setup a deep link handler
      [[Branch getInstance] initSessionWithLaunchOptions:launchOptions
                              andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {

        // start setting up the view controller hierarchy
        UINavigationController *navC = (UINavigationController *)self.window.rootViewController;
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *nextVC;

        // If the key 'pictureId' is present in the deep link dictionary
        // then load the picture screen with the appropriate picture
        NSString *pictureId = [params objectForKey:@"pictureId"];
        if (pictureId) {
          nextVC = [storyboard instantiateViewControllerWithIdentifier:@"PicVC"];
          [nextVC setNextPictureId:pictureId];
        } else {
          nextVC = [storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
        }

        // navigate!
        [navC setViewControllers:@[nextVC] animated:YES];
      }];

      return YES;
    }

Android

@Override
        public void onStart() {
            super.onStart();

            Branch.sessionBuilder(this).withCallback(new BranchReferralInitListener(){
                @Override
                public void onInitFinished(JSONObject referringParams, Branch.BranchError error) {
                    if (error == null) {
                        // params are the deep linked params associated with the link that the user clicked before showing up
                        // params will be empty if no data found
                        String pictureID = referringParams.optString(""picture_id"", """");
                        if (pictureID.equals("""")) {
                            startActivity(new Intent(this, HomeActivity.class));
                        }
                        else {
                            Intent i = new Intent(this, ViewerActivity.class);
                            i.putExtra(""picture_id"", pictureID);
                            startActivity(i);
                        }
                    } else {
                        Log.e(""MyApp"", error.getMessage());
                    }
                }
            }).withData(this.getIntent().getData()).init();
        }

Branch 添加的参数

除了在链接数据 dictionary 中指定的任何自定义 key/值对之外,每次会话初始化时,Branch 还返回一些其他有用的参数。即使用户未点击 Branch Link,每次初始化会话时也将返回这些参数。这是一个列表,以及每个代表什么的描述。

  • ~ 表示分析
  • + 表示 Branch 添加的信息
  • 该数据将无法用于来自 Facebook 或其他自归因平台的深度链接
  • For the curious, $ denotes reserved keywords used for controlling how the Branch service behaves. Read more about control parameters on the Configuring Links page

参数

意义

+is_first_session

表示这是第一个会话(安装)还是任何其他会话(重新安装,打开)

+clicked_branch_link

表示用户是否点击了触发该会话的 Branch Link

+match_guaranteed

关于匹配是否以100%的准确性进行的判断是对还是错

+referrer

点击链接的推荐人来源网址(如果已点击链接)

+click_timestamp

点击发生的时间戳记

+url

驱动安装/打开的链接的完整 URL(如果存在)(例如,yourapp.app.link / abcde12345)

~channel

在创建链接时指定共享链接的渠道

~feature

创建链接时指定的功能,例如邀请或分享

~tags

在链接创建时指定的任何标签

~campaign

与链接关联的 Campaign,在链接创建时指定

~creation_source

链接创建位置 ('API', 'Dashboard', 'SDK', 'iOS SDK', 'Android SDK', 或 'Web SDK')

~id

自动生成驱动安装/打开的链接的18位ID号(如果存在)(对于动态和3P链接为0)

稍后访问深度链接参数

您可以随时通过调用以下方法之一从 Branch singleton 中检索深度链接数据。如果要在提示用户登录后进行深度链接,则可以使用此方法。您可以在此处查看其他平台的代码片段。

获取最新的会话引用参数

这将从点击的最新链接中返回最新的深度链接数据集。如果您最小化该应用并重新打开它,则该会话将被清除,该数据也将被清除。

iOS

NSDictionary *params = [[Branch getInstance] getLatestReferringParams];

Android

JSONObject sessionParams = Branch.getInstance().getLatestReferringParams();

获取第一次会话引用参数

这将返回曾经引用用户的第一组深度链接数据。一旦为给定用户进行了设置 ,就永远无法更新。这对于推荐很有用。

iOS

NSDictionary *params = [[Branch getInstance] getFirstReferringParams];

Android

JSONObject installParams = Branch.getInstance().getFirstReferringParams();

选项 2:让 Branch 使用您现有的深度链接路由

If your app already supports deep linking using URI paths, you can populate the $deeplink_path$ios_deeplink_path or $android_deeplink_path link parameters with the URI path of the content to be displayed within the app. When the Branch SDK receives a link containing one of these parameters, it will automatically load the specified URI path.

🚧

对 iOS 不完全支持

Universal Links and Spotlight do not support deep linking via URI paths. If you use $deeplink_path or $ios_deeplink_path, you will need to implement some custom logic. Click here for more information.

如何将自定义深度链接路由插入 Branch Link

All of the examples below create links that will cause Branch to display myapp://content/1234 after launch. Please do not add the URI scheme on the $deeplink_path parameters; we automatically append the scheme specified in the Configuration page of the dashboard.

动态创建链接时
如果您通过附加查询参数重新创建链接,则只需将控制参数附加到 URL。请确保对所有内容进行 URL 编码,否则链接将断开。

"https://[branchsubdomain]?%24deeplink_path=content%2F1234"

使用移动 SDK 时

iOS

BranchLinkProperties *linkProperties = [[BranchLinkProperties alloc] init];
linkProperties.feature = @"sharing";
linkProperties.channel = @"facebook";
[linkProperties addControlParam:@"$deeplink_path" withValue:@"content/1234"];

Android

LinkProperties linkProperties = new LinkProperties()
.setChannel("facebook")
.setFeature("sharing")
.addControlParameter("$deeplink_path", "content/1234");

📘

在操作后台 (Dashboard) 创建 Quick Link 时

您可以通过将 key 和值插入深度链接数据(高级)部分来指定单个 Quick Link 的控制参数。

如何使用 Universal Links 或 App Links 链接处理 URI 路径

Because Universal Links, Spotlight and Android App Links do not use URI schemes for deep link routing. If you populate $deeplink_path$ios_deeplink_path or $android_deeplink_path with a URI path, you will need to a bit of additional work to ensure that Branch links route according to your original schema.

  1. 按照应用配置步骤中所述初始化会话。
  2. In the callback function, add some custom code to read the appropriate $deeplink_path parameter in the params
  3. 使用此值来调用现有的路由逻辑,以将用户路由到应用中的正确位置

Now whenever your app launches from a Branch link that has the product_picture key set in its data dictionary, this Activity will be displayed!