You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Expo托管工作流中配置react-native-branch?

在Expo托管工作流中配置react-native-branch的步骤

1. 安装依赖

执行命令安装所需包:

npx expo install expo-branch react-native-branch

2. 配置项目参数

在项目的app.json或app.config.js中,添加expo-branch插件配置,填入你的Branch密钥和关联域名:

{
  "expo": {
    "plugins": [
      [
        "expo-branch",
        {
          "apiKey": "你的Branch Live API密钥",
          "iosAppDomain": "你的iOS关联域名(如xxx.app.link)",
          "androidAppLinkDomains": ["你的Android关联域名"]
        }
      ]
    ]
  }
}

插件会自动完成原生层面的配置(包括Info.plist、AndroidManifest.xml等),无需手动修改这些文件。

3. 在App中初始化并监听Branch链接

在根App组件中,初始化Branch并设置链接监听:

import * as Branch from 'expo-branch';
import { useEffect } from 'react';

export default function App() {
  useEffect(() => {
    const initBranch = async () => {
      await Branch.initializeAsync();
      // 监听Branch链接打开事件
      Branch.subscribe(({ error, params }) => {
        if (error) {
          console.error('Branch初始化失败:', error);
          return;
        }
        if (params['+clicked_branch_link']) {
          // 处理通过Branch链接打开应用的逻辑
          console.log('Branch链接参数:', params);
        }
      });
    };

    initBranch();
  }, []);

  // 你的应用内容
  return (
    // ...
  );
}

4. 构建测试

因为是Expo托管工作流,需要通过构建工具生成安装包:

  • EAS Build构建:eas build --platform ios 或 eas build --platform android
  • 传统Expo构建:npx expo build:ios 或 npx expo build:android

构建完成后安装到设备,测试Branch链接是否能正常唤起应用并传递参数。

内容的提问来源于stack exchange,提问作者Ripas55

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 19:32:09