Перейти к содержимому
Busymate AI

Это руководство сейчас только на английском языке.

Руководства

Определите вошедших пользователей в React Native

Скопируйте один shim, внедрите его перед загрузкой контента и обработайте те же четыре сообщения идентификации, которые обрабатывает любой native bridge — один WebView на обеих платформах.

На этой странице

Busymate AI reaches a React Native app the same way it reaches any native shell: a small bridge answers who is signed in. React Native hides both platforms' native bridges behind one WebView component, so the frozen v2 bridge installs a single shim that speaks the same wire messages on either OS.

Current: Sign-in and identity, Setup C.

1. Copy the bridge files

Copy https://busymate.ai/sdk/v2/2.0.0/react-native/busymateBridge.js and https://busymate.ai/sdk/v2/2.0.0/core/busymateBridgeCore.js into your app. Create the bridge with createBusymateBridge({ assistant, appId, account, mint }): account returns your user id or null, and mint calls your own authenticated API with the request's nonce.

2. Inject before content loads, not after

Pass bridge.injectedJavaScriptBeforeContentLoaded to your WebView, give it ref={bridge.ref}, and set onMessage={bridge.onMessage}. Load the hosted chat URL from Console › Integration with channel=android or channel=ios.

Use injectedJavaScriptBeforeContentLoaded, never injectedJavaScript: the latter runs after load, so the very first identity ask can arrive before your bridge exists and gets no answer at all.

3. Tell the bridge when the user changes

Your mint calls your authenticated API on every ask, never a cached value. Call bridge.accountChanged() on login, token rotation, account switch and logout: it is the one signal, and account returns null once the user is signed out.

4. Try it with a fake sign-in first

Wire account to a toggle before your real auth store. Confirm the widget goes anonymous, then identified, then anonymous again as you flip it, with no reload.

Verify

  1. Signed out, open the screen: the mate runs an anonymous, guest session.
  2. Sign in inside your app, without reloading the WebView, and call accountChanged(): the same thread becomes identified.
  3. Background the app past the token's lifetime, then foreground it: the mate re-asks and gets a fresh answer, not a refused stale one.
  4. Sign out and reopen the screen: the previous customer's history is gone, not merely hidden.
  5. Run the Identity check against channel=android; React Native answers through the same wire shape the checker already grades.

Next steps

Вопросы

Do I need a separate integration for iOS builds of the same app?

No. The WebView component and this shim are the same on both platforms; only your native project's build configuration differs, not this bridge.

Why can't I use injectedJavaScript — it is simpler to reason about?

It runs once the page has already loaded, which is after the mate's first identity ask. A handler installed that late misses the one ask that matters most, the first one.

Does the shim work with Expo's managed WebView?

Any component built on react-native-webview accepts the same two props (injectedJavaScriptBeforeContentLoaded, onMessage), so the bridge asks nothing extra of your bundler or build tool.

What if my app never installs the shim?

The mate still runs, anonymously. A missing bridge is a supported, guest state — never a broken chat — until you wire identity in.