@braze/react-native-sdk

中文文档
24.0.0发布于 2026/10/26SEE LICENSE IN LICENSE.mdTypeScriptCommonJS零依赖近 30 天下载 88.1万

Braze SDK for React Native.

原始使用文档
文档目录30 节
  1. About the Braze React Native SDK
  2. What you can do
  3. Prerequisites
  4. Table of contents
  5. Installation
  6. Quick start
  7. Native setup
  8. Android
  9. iOS
  10. Configuration reference
  11. Android ( braze.xml )
  12. iOS ( Braze.Configuration )
  13. JavaScript / TypeScript API
  14. Core features
  15. User management
  16. In-app messages
  17. Content Cards
  18. Banners
  19. Push notifications
  20. Feature flags
  21. Analytics and purchases
  22. Data management and SDK state
  23. Events
  24. Integration notes
  25. Version support
  26. Braze Expo plugin
  27. Sample app
  28. Debugging and troubleshooting
  29. Additional resources
  30. Contact

Braze Logo Braze Logo

Braze React Native SDK latest npm

To learn more about the React Native SDK, see the following resources:

About the Braze React Native SDK

The Braze React Native SDK connects your iOS and Android apps to Braze: user profiles, messaging surfaces, analytics, and feature flags. It wraps the native Braze Swift SDK and Braze Android SDK behind a JavaScript API.

Initialization is JavaScript-driven: you set up native configuration (push, logging, delegates) in Android resources and iOS AppDelegate, then call Braze.initialize(apiKey, endpoint) from JavaScript to start the SDK. This gives you full control over when the SDK initializes and with which credentials. After initialization, call other SDK methods (for example changeUser, logCustomEvent) as needed.

What you can do

  • User management: Identify users, set profile fields, custom attributes, aliases, and subscription groups
  • In-app messages: Default Braze UI or custom handling via subscriptions and logging APIs
  • Content Cards: Default feed UI, or fetch cards and build your own UI
  • Banners: Placement-based HTML banners, including BrazeBannerView
  • Push notifications: Permission prompts, token registration, payload listeners (see platform notes below)
  • Feature flags: Refresh, read properties, log impressions
  • Analytics: Custom events, purchases, immediate flush
  • SDK controls: Enable/disable SDK, wipe local data, SDK Authentication signatures

Prerequisites

  • Braze account with app API key and SDK endpoint
  • React Native development environment (React Native environment setup)
  • iOS: Xcode, CocoaPods (cd ios && pod install)
  • Android: Android Studio / Gradle; Kotlin Gradle plugin as required by your React Native template
  • Push (if used): FCM (Android) and APNs (iOS) setup per push documentation

For credential locations in the dashboard, follow the integration overview.

Table of contents

  1. Installation
  2. Quick start
  3. Native setup
  4. Configuration reference
  5. JavaScript / TypeScript API
  6. Core features
  7. Events
  8. Integration notes
  9. Version support
  10. Braze Expo plugin
  11. Sample app
  12. Debugging and troubleshooting
  13. Additional resources

Installation

npm install @braze/react-native-sdk
# or:
# yarn add @braze/react-native-sdk

Quick start

This section shows the minimum setup required to initialize the Braze React Native SDK.

  1. Install the npm package (above).
  2. Complete native setup for Android and iOS (configuration, permissions, push if needed).
  3. Initialize the SDK from JavaScript and start using it:
import Braze from "@braze/react-native-sdk";

// Initialize the SDK — call early in your app lifecycle (e.g. in a useEffect).
// The API key and endpoint are passed from JavaScript; native configuration
// (push, logging, etc.) is applied automatically from your native setup.
Braze.initialize("<YOUR_API_KEY>", "<YOUR_SDK_ENDPOINT>");

Braze.changeUser("user-123");
Braze.logCustomEvent("button_clicked", { screen: "home" });

TypeScript typings ship with the package (src/index.d.ts on GitHub).

Calling Braze.initialize again with different credentials tears down the current instance and re-creates it, supporting mid-session re-initialization.


Native setup

Source of truth: Step-by-step screens, Gradle/CocoaPods changes, and the full list of Android XML keys are in the Braze React Native developer guide. The snippets below are minimal examples.

Android

  • Add the Kotlin Gradle plugin in your root build.gradle if your template does not already (versions depend on your React Native version).
  • Add a braze.xml resource file in res/values with your configuration. Enable delayed initialization so the SDK waits for Braze.initialize() from JavaScript before starting. Other configuration values (push, session timeout, etc.) are still read from this file and applied at initialization time.
  • Ensure basic permissions such as INTERNET and ACCESS_NETWORK_STATE in AndroidManifest.xml.
  • For push, complete FCM integration and any Braze-specific sender ID / registration flags described in the docs.
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <!-- Enable delayed initialization so the SDK starts when
       Braze.initialize() is called from JavaScript. -->
  <bool name="com_braze_enable_delayed_initialization">true</bool>

  <!-- Additional native configuration (applied at initialization time) -->
  <bool name="com_braze_firebase_cloud_messaging_registration_enabled">true</bool>
  <string translatable="false" name="com_braze_firebase_cloud_messaging_sender_id">YOUR_SENDER_ID</string>
</resources>

Note: The API key and endpoint are no longer set in braze.xml — they are passed from JavaScript via Braze.initialize(apiKey, endpoint).

iOS

cd ios && pod install

Use BrazeReactInitializer.configure in your AppDelegate to register native configuration. The closures you provide are stored and applied later when Braze.initialize(apiKey, endpoint) is called from JavaScript.

import BrazeKit
import braze_react_native_sdk

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  static var braze: Braze? = nil

  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    // Register native configuration for when JS calls Braze.initialize().
    BrazeReactInitializer.configure { config in
      config.logger.level = .info
      config.push.automation = true
    } postInitialization: { braze in
      AppDelegate.braze = braze
    }

    // ... React Native setup
    return true
  }
}
  • configure closure: receives a Braze.Configuration and lets you set native configuration properties (logging, push, sessions, etc.). The API key and endpoint are provided from JavaScript — you do not set them here.
  • postInitialization closure (optional): receives the live Braze instance after creation, for setup that requires the instance (e.g. storing a reference, setting delegates).

Note: BrazeReactInitializer.configure is a Swift-first API that replaces the deprecated BrazeReactBridge.initBraze(_:). It also resolves a Swift type-resolution issue with Braze.Configuration in the Objective-C bridge.


Configuration reference

In React Native, configuration is native: Android reads res/values/braze.xml, and iOS uses closures registered via BrazeReactInitializer.configure. Both are applied when Braze.initialize(apiKey, endpoint) is called from JavaScript.

Android (braze.xml)

Defaults live in XML; BrazeConfig.Builder can override them at startup. The authoritative list of keys and types is in the Android SDK integration guide and in BrazeConfigurationProvider (each Kotlin property corresponds to documented com_braze_* resources).

Commonly used entries:

KeyResource typeDescription
com_braze_enable_delayed_initializationboolRequired. Set to true so the SDK waits for Braze.initialize() from JavaScript.
com_braze_api_keystringNot needed when using Braze.initialize() from JavaScript (credentials are passed from JS). Only required for legacy native-first initialization.
com_braze_custom_endpointstringNot needed when using Braze.initialize() from JavaScript. Only required for legacy native-first initialization.
com_braze_server_targetstringOptional cluster / environment selector (e.g. some internal or staging builds). Prefer com_braze_custom_endpoint for production unless your Braze integration specifies otherwise.
com_braze_firebase_cloud_messaging_registration_enabledboolWhen true, Braze registers for FCM (typical push setup).
com_braze_firebase_cloud_messaging_sender_idstringFCM sender ID when automatic registration is enabled.
com_braze_handle_push_deep_links_automaticallyboolLet Braze open push deep links automatically.
com_braze_trigger_action_minimum_time_interval_secondsintegerMinimum seconds between in-app message trigger actions.
OthervariousAdditional keys not shown here (session timeout, geofences, location, notification defaults, device allowlists, delayed initialization, SDK Authentication, and more). See BrazeConfigurationProvider and the Android SDK integration guide.

iOS (Braze.Configuration)

Set native configuration properties in the configure closure passed to BrazeReactInitializer.configure. The closure receives a Braze.Configuration instance — the API key and endpoint are set automatically from the JavaScript Braze.initialize call. Full details: Braze.Configuration and nested types api, push, logger, location.

AreaMembers (representative)Notes
Credentialsapi.key, api.endpointSet automatically from Braze.initialize(apiKey, endpoint) in JavaScript. Do not set these in the configure closure.
Logginglogger.levelVerbose logging is for development; reduce noise in production.
Pushpush.automation, push.appGroup, …Automation simplifies registration; appGroup needed for Push Stories / extensions when used.
In-app messagestriggerMinimumTimeIntervalDefault 30 seconds between triggers.
SessionssessionTimeoutInactivity before a new session (see Braze session docs).
Privacy / dataapi.trackingPropertyAllowList, devicePropertyAllowList, api.sdkAuthenticationAlign with privacy manifest and SDK Authentication product settings.
Networkingapi.requestPolicy, api.flushIntervalRequest retry policy and flush cadence.
Push subscriptionoptInWhenPushAuthorizedWhen true, subscription can move to opted-in after the user authorizes notifications.
IAM + user changespreventInAppMessageDisplayForDifferentUserReduces mismatched IAM if the user ID changes.
OtherforwardUniversalLinks, ephemeralEvents, useUUIDAsDeviceId, …See Swift documentation for full behavior.

The React Native bridge sets React-specific api.sdkFlavor / SDK metadata on init; do not override those unless Braze documentation instructs you to.


JavaScript / TypeScript API

The package default export is the Braze class with static methods (for example Braze.changeUser, Braze.logPurchase). Constants such as Braze.Events, Braze.Genders, and Braze.NotificationSubscriptionTypes are attached to the same export.


Core features

User management

import Braze from "@braze/react-native-sdk";

Braze.changeUser("user-123");
Braze.setEmail("[email protected]");
Braze.setCustomUserAttribute("plan", "premium");
Braze.addAlias("external_id", "marketing_id");
Braze.addToSubscriptionGroup("NEWSLETTER_GROUP_UUID");

Optional SDK Authentication: pass a signature as the second argument to changeUser, or call Braze.setSdkAuthenticationSignature(signature) when enabled in the dashboard.

In-app messages

  • With the default Braze UI, follow the in-app message documentation; you typically do not need to call subscribeToInAppMessage only to show default UI.
  • For custom handling, subscribe with useBrazeUI: false, then log impressions/clicks as needed:
Braze.subscribeToInAppMessage(false, (event) => {
  const msg = event.inAppMessage;
  // Render your own UI from msg.message, msg.buttons, etc.
  Braze.logInAppMessageImpression(msg);
});

Content Cards

const cards = await Braze.getCachedContentCards();
Braze.requestContentCardsRefresh();
Braze.launchContentCards(); // default Braze UI

Braze.logContentCardImpression(cardId);
Braze.logContentCardClicked(cardId);

Listen for updates with Braze.addListener(Braze.Events.CONTENT_CARDS_UPDATED, ...).

Banners

import Braze from "@braze/react-native-sdk";

Braze.requestBannersRefresh(["homepage_banner"]);
const banner = await Braze.getBanner("homepage_banner");

// Or use the native Banner view:
// <Braze.BrazeBannerView placementId="homepage_banner" />

Push notifications

Braze.requestPushPermission({
  alert: true,
  badge: true,
  sound: true,
});
// Token registration is usually handled natively; see docs for your setup.
Braze.registerPushToken(token);
  • getInitialPushPayload: use when the app opens from a notification to avoid RN Linking race conditions. It requires native setup on each platform.
    • iOS, with the app delegate launch options: call populateInitialPayloadFromLaunchOptions: on BrazeReactUtils from application:didFinishLaunchingWithOptions:.
    • iOS, with the scene life cycle: call populateInitialPayloadFromNotificationResponse: on BrazeReactUtils from scene:willConnectToSession:options:.
    • Android: call BrazeReactUtils.populateInitialPushPayloadFromIntent from MainActivity.onCreate().
  • Braze.addListener(Braze.Events.PUSH_NOTIFICATION_EVENT, ...) is Android-only per the public typings.

Feature flags

const flag = await Braze.getFeatureFlag("new_checkout");
if (flag?.enabled) {
  const rollout = flag.getNumberProperty("rollout_percentage") ?? 0;
}
Braze.refreshFeatureFlags();
Braze.logFeatureFlagImpression("new_checkout");

Analytics and purchases

Braze.logCustomEvent("purchase_completed", { sku: "sku-1" });
Braze.logPurchase("sku-1", "29.99", "USD", 1, { source: "cart" });
Braze.requestImmediateDataFlush();

Note: logPurchase takes price as a string (see typings).

Data management and SDK state

changeUser only tells Braze which user ID to attribute new activity to. It does not clear cached SDK data on the device. There is no separate “logout” API: if you need a traditional sign-out (clear local Braze state so the prior user’s cached profile, messages, and tokens are gone on this install), you typically use wipeData(). This is a full local reset.

Braze.wipeData();
Braze.disableSDK();
Braze.enableSDK();

wipeData() — Clears Braze’s local data for this install (cached user/session/card state, push token association, etc.). Use for sign-out–style behavior when you must not leave the previous user’s Braze state on device, plus “delete my data on this device”, QA resets without reinstalling, or strict privacy flows. changeUser alone does not perform that cleanup—it only sets which user ID receives new events. On iOS, behavior may differ from Android (e.g. interaction with SDK disabled state); see Braze’s native docs if you ship this in production.

disableSDK() — Stops the SDK from operating (no collection/forwarding as configured). Use for user opt-out toggles, restricted modes (compliance, kids’ settings), or debugging without removing the dependency.

enableSDK() — Turns the SDK back on after disableSDK(). On iOS, re-enabling may not apply until the next app launch; verify in Braze Swift/iOS documentation before relying on immediate re-enable.


Events

Subscribe with Braze.addListener(event, callback). The call returns a subscription object; call .remove() on it to stop listening.

Setting up a listener:

import Braze from "@braze/react-native-sdk";

const subscription = Braze.addListener(
  Braze.Events.CONTENT_CARDS_UPDATED,
  (update) => {
    console.log("Content cards:", update.cards);
  }
);

Removing the listener:

subscription.remove();

In a React component, store the subscription and call .remove() in your cleanup (e.g. the return of a useEffect):

useEffect(() => {
  const sub = Braze.addListener(Braze.Events.CONTENT_CARDS_UPDATED, (update) => {
    setCards(update.cards);
  });
  return () => sub.remove();
}, []);
Event constantPayload (summary)
Braze.Events.CONTENT_CARDS_UPDATEDLatest content cards
Braze.Events.BANNER_CARDS_UPDATEDLatest banners
Braze.Events.FEATURE_FLAGS_UPDATEDFeature flag array
Braze.Events.IN_APP_MESSAGE_RECEIVEDIn-app message event
Braze.Events.SDK_AUTHENTICATION_ERRORSDK auth error details
Braze.Events.PUSH_NOTIFICATION_EVENTPush payload (Android only)

Integration notes

  • Expo: use the Braze Expo plugin to avoid manual native wiring where possible.
  • New Architecture / Turbo Modules: supported on recent plugin versions; follow the developer guide and sample AppDelegate / Gradle settings if you migrate.
  • Privacy (iOS): methods such as updateTrackingPropertyAllowList support privacy manifest–related configuration; see Swift privacy manifest.
  • Jest: mock react-native native modules or the Braze Turbo module. Unit tests are not published in this repository.

Version support

[!NOTE] This SDK has been tested with React Native version 0.88.0.

The following table lists supported React Native versions by Braze plugin release.

Braze pluginReact NativeNew Architecture
9.0.0+≥ 0.71Yes
6.0.0+≥ 0.68Yes (≥ 0.70.0)
2.0.0+≥ 0.68Yes
≤ 1.41.0≤ 0.71No

Also respect native SDK requirements:


Braze Expo plugin

For Expo-managed workflows, see the Braze Expo plugin repository.


Sample app

BrazeProject in this repository is a full sample (user management, content cards, feature flags, banners, etc.).

cd BrazeProject/
yarn install
npx react-native start

iOS (from BrazeProject):

cd ios && pod install && cd ..
npx react-native run-ios

Use RCT_NEW_ARCH_ENABLED=0 pod install if you need the legacy architecture.

Android (from BrazeProject):

npx react-native run-android

Debugging and troubleshooting

Enable Braze logging in native configuration during development so the SDK writes to the system console (Xcode / Android Logcat). This helps verify initialization, user changes, and event delivery.

  • iOS — In the configure closure passed to BrazeReactInitializer.configure, set config.logger.level = .debug (or .info). Reduce or disable in production so logs are not visible to users.
  • Android — Use the com_braze_logger_initial_log_level resource in braze.xml or set the equivalent on BrazeConfig.Builder (see BrazeConfigurationProvider). Use a non-verbose level or remove the override before release.

For deeper troubleshooting (network, session, or campaign behavior), see the Braze React Native developer guide and the native SDK docs (Swift · Android).


Additional resources

Contact

For questions, contact Braze Technical Support for assistance.

版本与发布记录

@braze/react-native-sdk 共发布 51 个版本,首次发布于 2023/02/09;过去 12 个月发布了 11 个版本。

版本发布日期距今
24.0.0latest2026/10/2613 天前
23.1.02026/09/221 个月前
23.0.02026/08/222 个月前
22.0.02026/06/294 个月前
21.1.02026/06/234 个月前
21.0.02026/06/094 个月前

关于 @braze/react-native-sdk 的常见问题

@braze/react-native-sdk 是什么?

Braze SDK for React Native.

如何安装 @braze/react-native-sdk?

在项目目录运行 npm install @braze/react-native-sdk,或使用 pnpm add / yarn add / bun add。

@braze/react-native-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@braze/react-native-sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@braze/react-native-sdk 可以商用吗?

@braze/react-native-sdk 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@braze/react-native-sdk 的下载量有多少?

根据 npm 官方下载统计,@braze/react-native-sdk 在 2026/10/09 – 2026/11/07 这 30 天内被下载 881,408 次,日均约 29,380 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@braze/react-native-sdk 还在维护吗?

最新版本 24.0.0 发布于 2026/10/26(13 天前);至今共发布 51 个版本,其中过去 12 个月发布了 11 个;首次发布于 2023/02/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。