|
| 1 | +# Klaviyo React Native SDK Example App |
| 2 | + |
| 3 | +The example app is a reference integration of the Klaviyo React Native SDK. It demonstrates |
| 4 | +a basic Klaviyo integration with all major SDK features including: |
| 5 | + |
| 6 | +- Initialization |
| 7 | +- Analytics |
| 8 | +- Push Notifications |
| 9 | +- Geofencing |
| 10 | +- Deep Linking |
| 11 | +- In-App Forms |
| 12 | + |
| 13 | +## Integration Step Comments |
| 14 | + |
| 15 | +The example source is annotated with `RN|iOS|Android Installation Step` comments that mark |
| 16 | +the code an integrator needs to replicate in their own app. Grep for any of those prefixes |
| 17 | +across `example/` to walk the integration surface: |
| 18 | + |
| 19 | +- `RN Installation Step` — `example/src/App.tsx` and `example/index.js` (SDK init, |
| 20 | + API key sourcing, optional `@react-native-firebase/messaging` background handler) |
| 21 | +- `iOS Installation Step` — `example/ios/KlaviyoReactNativeSdkExample/AppDelegate.{h,mm}`, |
| 22 | + `PushNotificationsHelper.swift`, `NotificationService.swift`, and the Podfile |
| 23 | +- `Android Installation Step` — `example/android/build.gradle`, `app/build.gradle`, |
| 24 | + `AndroidManifest.xml`, and `MainActivity.kt` |
| 25 | + |
| 26 | +## Getting Started |
| 27 | + |
| 28 | +### 1. Configure Your API Key |
| 29 | + |
| 30 | +The app loads your Klaviyo public API key from a gitignored `.env` file via |
| 31 | +[`react-native-dotenv`](https://github.com/goatandsheep/react-native-dotenv). |
| 32 | + |
| 33 | +1. Copy the template: `cp .env.template .env` |
| 34 | +2. Edit `.env` and set `KLAVIYO_API_KEY=` to your actual public API key (`.env` is gitignored) |
| 35 | +3. Restart Metro with `yarn start --reset-cache` so the metro picks up the new value |
| 36 | + |
| 37 | +> **If you hit "Klaviyo API key not configured" after setting up `.env`:** Metro inlines |
| 38 | +> `.env` values at bundle time via `react-native-dotenv`. If Metro was already running when |
| 39 | +> you created or edited `.env`, it will serve a cached bundle with `KLAVIYO_API_KEY` still |
| 40 | +> undefined. Stop Metro and restart it with `yarn start --reset-cache` (from `example/`). |
| 41 | +
|
| 42 | +### 2. Install Dependencies |
| 43 | + |
| 44 | +The one-liner, run from the repo root: |
| 45 | + |
| 46 | +```bash |
| 47 | +# From the repo root |
| 48 | +yarn example setup |
| 49 | + |
| 50 | +# This alias runs: |
| 51 | +# yarn install --immutable (repo root) |
| 52 | +# bundle install (example/) |
| 53 | +# bundle exec pod install (example/ios/) |
| 54 | +``` |
| 55 | + |
| 56 | +### 3. Run the App |
| 57 | + |
| 58 | +Preferred — from the repo root (yarn workspaces entry point): |
| 59 | + |
| 60 | +```bash |
| 61 | +# From the repo root |
| 62 | +yarn example start |
| 63 | +yarn example android |
| 64 | +yarn example ios |
| 65 | +``` |
| 66 | + |
| 67 | +## Push Notifications |
| 68 | + |
| 69 | +Push notifications are handled via `@react-native-firebase/messaging` in the |
| 70 | +JavaScript layer — the Klaviyo SDK is initialized from JS, and Firebase manages |
| 71 | +push tokens cross-platform. |
| 72 | + |
| 73 | +### Platform-specific Firebase setup |
| 74 | + |
| 75 | +**Android** builds and runs out of the box without Firebase configured. The |
| 76 | +`com.google.gms.google-services` plugin is conditionally applied only when |
| 77 | +`google-services.json` is present. Without the file, push features are dormant |
| 78 | +but everything else (profile, events, in-app forms, geofencing) works normally. |
| 79 | + |
| 80 | +- To enable push: drop a real `google-services.json` from your Firebase project |
| 81 | + into `example/android/app/`. The file is gitignored. Ensure the |
| 82 | + `applicationId` in `example/android/app/build.gradle` |
| 83 | + (`com.klaviyoreactnativesdkexample`) is registered as an Android app in your |
| 84 | + Firebase project. |
| 85 | + |
| 86 | +**iOS** requires a `GoogleService-Info.plist` at `example/ios/` to build at all — the Xcode |
| 87 | +project's Resources build phase references it unconditionally. Two options: |
| 88 | + |
| 89 | +- **To enable push:** drop a real `GoogleService-Info.plist` from your Firebase |
| 90 | + project at `example/ios/GoogleService-Info.plist` (gitignored). Ensure your |
| 91 | + iOS bundle identifier is registered in the same Firebase project. |
| 92 | +- **To build without push:** write a stub plist at the same path. The values |
| 93 | + below are format-valid (39-char `API_KEY` starting with `A`, full |
| 94 | + `GOOGLE_APP_ID` format), so `FirebaseApp.configure()` succeeds at launch. |
| 95 | + Firebase will log a benign "couldn't register with backend" warning at |
| 96 | + runtime since the project isn't real — that's expected, not a bug. The rest |
| 97 | + of the app works normally. |
| 98 | + <details> |
| 99 | + <summary>Stub <code>GoogleService-Info.plist</code></summary> |
| 100 | + |
| 101 | + ```xml |
| 102 | + <?xml version="1.0" encoding="UTF-8"?> |
| 103 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 104 | + <plist version="1.0"> |
| 105 | + <dict> |
| 106 | + <key>BUNDLE_ID</key><string>com.klaviyoreactnativesdkexample</string> |
| 107 | + <key>GOOGLE_APP_ID</key><string>1:000000000000:ios:0000000000000000000000</string> |
| 108 | + <key>API_KEY</key><string>AIzaSyA00000000000000000000000000000000</string> |
| 109 | + <key>GCM_SENDER_ID</key><string>000000000000</string> |
| 110 | + <key>PROJECT_ID</key><string>stub-project</string> |
| 111 | + <key>STORAGE_BUCKET</key><string>stub-project.appspot.com</string> |
| 112 | + <key>PLIST_VERSION</key><string>1</string> |
| 113 | + <key>IS_GCM_ENABLED</key><true/> |
| 114 | + <key>IS_ADS_ENABLED</key><false/> |
| 115 | + <key>IS_ANALYTICS_ENABLED</key><false/> |
| 116 | + <key>IS_APPINVITE_ENABLED</key><false/> |
| 117 | + <key>IS_SIGNIN_ENABLED</key><false/> |
| 118 | + </dict> |
| 119 | + </plist> |
| 120 | + ``` |
| 121 | + |
| 122 | + </details> |
| 123 | + |
| 124 | +### Native push (alternative) |
| 125 | + |
| 126 | +For apps that prefer to handle push tokens natively (e.g., brownfield apps), the |
| 127 | +Klaviyo iOS and Android SDKs both have their own native push APIs. See the |
| 128 | +`AppDelegate.mm` comments on iOS and `MainApplication.kt` comments on Android for |
| 129 | +pointers, plus the platform SDKs' READMEs: |
| 130 | + |
| 131 | +- iOS: https://github.com/klaviyo/klaviyo-swift-sdk#push-notifications |
| 132 | +- Android: https://github.com/klaviyo/klaviyo-android-sdk#push-notifications |
| 133 | + |
| 134 | +Note: both platforms require native-layer push-open handling regardless of how tokens |
| 135 | +are managed: |
| 136 | + |
| 137 | +- **Android:** `Klaviyo.handlePush(intent)` in `MainActivity.kt` (`onCreate` + |
| 138 | + `onNewIntent`) — covers cold-start taps and resume-from-background taps. |
| 139 | +- **iOS:** `[PushNotificationsHelper handleReceivingPushWithResponse:...]` inside |
| 140 | + the `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:` |
| 141 | + delegate in `AppDelegate.mm`, plus the `getLaunchOptionsWithURL` helper to |
| 142 | + forward any cold-start deep-link URL (React Native issue |
| 143 | + [#32350](https://github.com/facebook/react-native/issues/32350)). |
0 commit comments