|
17 | 17 | - [Android](#android-1)
|
18 | 18 | - [iOS](#ios-1)
|
19 | 19 | - [Initialization](#initialization)
|
| 20 | + - [React Native Initialization](#react-native-initialization) |
| 21 | + - [Native Initialization](#native-initialization) |
20 | 22 | - [Identifying a Profile](#identifying-a-profile)
|
21 | 23 | - [Reset Profile](#reset-profile)
|
22 | 24 | - [Anonymous Tracking](#anonymous-tracking)
|
|
25 | 27 | - [Prerequisites](#prerequisites)
|
26 | 28 | - [Setup](#setup)
|
27 | 29 | - [Collecting Push Tokens](#collecting-push-tokens)
|
| 30 | + - [React Native Token Collection](#react-native-token-collection) |
| 31 | + - [Native Token Collection](#native-token-collection) |
28 | 32 | - [Receiving Push Notifications](#receiving-push-notifications)
|
29 | 33 | - [Rich Push](#rich-push)
|
30 | 34 | - [Tracking Open Events](#tracking-open-events)
|
@@ -86,9 +90,11 @@ yarn add klaviyo-react-native-sdk
|
86 | 90 | ```
|
87 | 91 |
|
88 | 92 | ### Example App
|
| 93 | + |
89 | 94 | We have included a bare-bones example app in this repository for reference of how to integrate with our SDK.
|
90 | 95 | It is primarily intended to give code samples such as how and where to `initialize` or how to implement notification
|
91 | 96 | delegate methods on iOS. To actually run the example app:
|
| 97 | + |
92 | 98 | - Clone this repository
|
93 | 99 | - From the root directory, run `yarn example-setup`. This is an alias that will do the following:
|
94 | 100 | - Run `yarn install --immutable` from the root directory
|
@@ -129,85 +135,28 @@ Install [Cocoapods](https://cocoapods.org/) if you have not already.
|
129 | 135 |
|
130 | 136 | ## Initialization
|
131 | 137 |
|
132 |
| -The SDK must be initialized with the short alphanumeric |
133 |
| -[public API key](https://help.klaviyo.com/hc/en-us/articles/115005062267#difference-between-public-and-private-api-keys1) |
134 |
| -for your Klaviyo account, also known as your Site ID. Initialization is done in the native layer, and must occur before |
135 |
| -any other SDK methods can be invoked. Follow the native SDK instructions for initialization, and refer to the |
136 |
| -[example app](./example) in this repository for guidance: |
137 |
| - |
138 |
| -- [Android SDK instructions](https://github.com/klaviyo/klaviyo-android-sdk#Initialization), and |
139 |
| - [example app `MainApplication.kt`](./example/android/app/src/main/java/com/klaviyoreactnativesdkexample/MainApplication.kt#L39) |
140 |
| -- [iOS SDK instructions](https://github.com/klaviyo/klaviyo-swift-sdk#Initialization), and |
141 |
| - [example app `AppDelegate.mm`](./example/ios/KlaviyoReactNativeSdkExample/AppDelegate.mm#L14) |
142 |
| - |
143 |
| -### Example App |
144 |
| -We have included a bare-bones example app in this repository for reference of how to integrate with our SDK. |
145 |
| -It is primarily intended to give code samples such as how and where to `initialize` or how to implement notification |
146 |
| -delegate methods on iOS. To actually run the example app: |
147 |
| -- Clone this repository |
148 |
| -- From the root directory, run `yarn example-setup`. This is an alias that will do the following: |
149 |
| - - Run `yarn install --immutable` from the root directory |
150 |
| - - Navigate to the `example` directory and run `bundle install` |
151 |
| - - Navigate to the `example/ios` directory and run `bundle exec pod install` |
152 |
| -- From the project's root directory, run `yarn example start` to start the example application. Follow the |
153 |
| - metro instructions from here, i.e. press `i` to run on iOS or `a` to run on Android. |
154 |
| - |
155 |
| -### Android |
156 |
| - |
157 |
| -Android installation requirements may vary depending upon your project configuration and other dependencies. |
158 |
| -The Klaviyo React Native SDK's `build.gradle` file exposes transitive dependencies upon the Klaviyo Android SDK, |
159 |
| -so you can import Android Klaviyo SDK references from your Kotlin/Java files without modifying your gradle configuration. |
160 |
| - |
161 |
| -#### React Native 0.73.x |
162 |
| - |
163 |
| -There are no additional installation requirements. Android support is fully tested and verified, |
164 |
| -including `minSdkVersion=23`. |
165 |
| - |
166 |
| -#### React Native 0.68.x - 0.72.x |
167 |
| - |
168 |
| -We have successfully compiled the Klaviyo React Native SDK in a bare React Native template app for these versions |
169 |
| -with the following modifications to the `android/build.gradle` file: |
170 |
| - |
171 |
| -- Set `compileSdkVersion=34` |
172 |
| -- Set `minSdkVersion=23` |
173 |
| - |
174 |
| -#### React Native <= 0.67.x |
175 |
| - |
176 |
| -We are actively working to verify compatibility with these versions. If you encounter issues, please file an issue. |
177 |
| - |
178 |
| -### iOS |
| 138 | +The SDK must be initialized with the short alphanumeric [public API key](https://help.klaviyo.com/hc/en-us/articles/115005062267#difference-between-public-and-private-api-keys1) |
| 139 | +for your Klaviyo account, also known as your Site ID. |
179 | 140 |
|
180 |
| -After installing the npm package, run the following command in the `ios` directory of your React Native project. |
181 |
| -Install [Cocoapods](https://cocoapods.org/) if you have not already. |
| 141 | +Initialize _must_ be called prior to invoking any other SDK methods so that Klaviyo SDK can track profiles, events and push tokens toward the correct Klaviyo account. |
| 142 | +Any SDK operations invoked before initialize will be dropped, and result in a logged error. |
182 | 143 |
|
183 |
| -#### React Native 0.68.x - 0.72.x |
184 |
| -We have successfully compiled the Klaviyo React Native SDK in a bare React Native template app for these versions |
185 |
| -with the following modifications to the `android/build.gradle` file: |
186 |
| -- Set `compileSdkVersion=34` |
187 |
| -- Set `minSdkVersion=23` |
| 144 | +You can call `initialize` from your app's React Native layer or from the platform-specific native code. |
| 145 | +This decision is dependent on your app's architecture. It is not required to initialize the SDK in both places! |
| 146 | +Note: It is safe to re-initialize, e.g. if your app needs to connect to more than one Klaviyo account. |
188 | 147 |
|
189 |
| -#### React Native <= 0.67.x |
190 |
| -We are actively working to verify compatibility with these versions. If you encounter issues, please file an issue. |
| 148 | +### React Native Initialization |
191 | 149 |
|
192 |
| -#### Android Troubleshooting |
193 |
| -- We have seen projects, particularly on react-native versions `0.72.x` and `0.71.x`, that required a `minSdkVersion` |
194 |
| - of `24`, despite the Klaviyo Android SDK supporting API 23+. If you encounter this, please file an issue in our |
195 |
| - repository and provide version numbers of your react-native dependencies. |
| 150 | +Below is an example of how to initialize the SDK from your React Native code: |
196 | 151 |
|
197 |
| -### iOS |
198 |
| -After installing the npm package, run the following command in the `ios` directory of your React Native project. |
199 |
| -Install [Cocoapods](https://cocoapods.org/) if you have not already. |
200 |
| -```sh |
201 |
| -pod install |
| 152 | +```typescript |
| 153 | +import { Klaviyo } from 'klaviyo-react-native-sdk'; |
| 154 | +Klaviyo.initialize('YOUR_KLAVIYO_PUBLIC_API_KEY'); |
202 | 155 | ```
|
203 |
| -> ℹ️ if the above command is outputting version mismatch errors for `KlaviyoSwift`, please run `pod update KlaviyoSwift` as indicated in the error message to update your local pods spec repo. |
204 | 156 |
|
205 |
| -## Initialization |
| 157 | +### Native Initialization |
206 | 158 |
|
207 |
| -The SDK must be initialized with the short alphanumeric |
208 |
| -[public API key](https://help.klaviyo.com/hc/en-us/articles/115005062267#difference-between-public-and-private-api-keys1) |
209 |
| -for your Klaviyo account, also known as your Site ID. Initialization is done in the native layer, and must occur before |
210 |
| -any other SDK methods can be invoked. Follow the native SDK instructions for initialization, and refer to the |
| 159 | +Follow the native SDK instructions for initialization, and refer to the |
211 | 160 | [example app](./example) in this repository for guidance:
|
212 | 161 |
|
213 | 162 | - [Android SDK instructions](https://github.com/klaviyo/klaviyo-android-sdk#Initialization), and
|
@@ -333,17 +282,111 @@ Refer to the following README sections on push setup:
|
333 | 282 |
|
334 | 283 | ### Collecting Push Tokens
|
335 | 284 |
|
336 |
| -Push tokens must be collected in the native layer. Follow the platform-specific instructions below: |
| 285 | +Push tokens can be collected either from your app's react native code or in the native code. Below sections discuss both approaches, and |
| 286 | +you are free to pick one that best suits your app's architecture. Note that doing this in one location is sufficient. |
| 287 | + |
| 288 | +#### React Native Token Collection |
| 289 | + |
| 290 | +In order to collect the APNs push token in your React Native code you need to: |
| 291 | + |
| 292 | +1. Import a library such as [`@react-native-firebase/messaging`](https://www.npmjs.com/package/@react-native-firebase/messaging) to your react native project. The below instructions are specific for `@react-native-firebase/messaging` library. |
| 293 | +2. Import Firebase iOS SDK to your iOS project. Setup instructions can be found [here](https://firebase.google.com/docs/ios/setup). |
| 294 | +3. In order for the `UNUserNotificationCenter` delegate methods to be called in `AppDelegate`, method swizzling should be disabled for the Firebase SDK. For more information on this, |
| 295 | + please refer to the [Firebase documentation](https://firebase.google.com/docs/cloud-messaging/ios/client). Disabling method swizzling be done by adding the following to your `Info.plist`: |
| 296 | + |
| 297 | +```xml |
| 298 | +<key>FirebaseAppDelegateProxyEnabled</key> |
| 299 | +<false/> |
| 300 | +``` |
| 301 | + |
| 302 | +4. In `application:didRegisterForRemoteNotificationsWithDeviceToken:` method in your `AppDelegate.m` file, you can add the following code to set the push token to the firebase SDK: |
| 303 | + |
| 304 | +```objective-c |
| 305 | +// since we disbaled swizzling, we have to manually set this |
| 306 | +FIRMessaging.messaging.APNSToken = deviceToken; |
| 307 | +``` |
| 308 | + |
| 309 | +5. Finally, in your React Native code, you can collect & set the push token as follows: |
| 310 | + |
| 311 | +```typescript |
| 312 | +import messaging from '@react-native-firebase/messaging'; |
| 313 | +import { Klaviyo } from 'klaviyo-react-native-sdk'; |
| 314 | +import { Platform } from 'react-native'; |
| 315 | + |
| 316 | +const fetchAndSetPushToken = async () => { |
| 317 | + try { |
| 318 | + let deviceToken: string | null = null; |
| 319 | + if (Platform.OS === 'android') { |
| 320 | + deviceToken = await messaging().getToken(); |
| 321 | + console.log('FCM Token:', deviceToken); |
| 322 | + } else { |
| 323 | + deviceToken = await messaging().getAPNSToken(); |
| 324 | + console.log('APNs Token:', deviceToken); |
| 325 | + } |
| 326 | + |
| 327 | + if (deviceToken != null && deviceToken.length > 0) { |
| 328 | + Klaviyo.setPushToken(deviceToken!); |
| 329 | + } |
| 330 | + } catch (error) { |
| 331 | + console.error('Error in fetchAndSetPushToken:', error); |
| 332 | + } |
| 333 | +}; |
| 334 | +``` |
| 335 | + |
| 336 | +For android token collection, there isn't any additional setup required on the native side. The above code should work as is. |
| 337 | + |
| 338 | +#### Native Token Collection |
| 339 | + |
| 340 | +Follow the platform-specific instructions below: |
337 | 341 |
|
338 | 342 | - [Android](https://github.com/klaviyo/klaviyo-android-sdk#Collecting-Push-Tokens)
|
339 | 343 | - [iOS](https://github.com/klaviyo/klaviyo-swift-sdk#Collecting-Push-Tokens)
|
340 | 344 |
|
341 | 345 | #### Notification Permission
|
342 | 346 |
|
343 |
| -Requesting user permission to display notifications can be managed in the native layer as instructed in our native SDK |
344 |
| -documentation, or with a third party library that provides cross-platform permissions APIs. If you opt for a |
345 |
| -cross-platform permission solution, you will still need to provide the Klaviyo SDK with the push token from the |
346 |
| -native layer after a permission change. |
| 347 | +Requesting user permission to display notifications can be managed in: |
| 348 | + |
| 349 | +1. The native layer as instructed in our native SDK documentation, |
| 350 | + |
| 351 | + - [Android](https://github.com/klaviyo/klaviyo-android-sdk#collecting-push-tokens) |
| 352 | + - [iOS](https://github.com/klaviyo/klaviyo-swift-sdk?tab=readme-ov-file#request-push-notification-permission) |
| 353 | + |
| 354 | + If you requested permission using native code then continue using Klaviyo's native platform SDKs `setToken` method to inform the SDK of permission change. |
| 355 | + |
| 356 | +2. Leveraging a third party library that provides cross-platform permissions APIs like firebase [`react-native-firebase/messaging`](https://www.npmjs.com/package/@react-native-firebase/messaging). If you opt for a |
| 357 | + cross-platform permission solution, you can now call the Klaviyo's react native SDK's `setToken` method to refresh the token's enablement status. |
| 358 | + |
| 359 | + Below is an example of how to use `@react-native-firebase/messaging` to request permission and set the token: |
| 360 | + |
| 361 | +```typescript |
| 362 | +import messaging from '@react-native-firebase/messaging'; |
| 363 | + |
| 364 | +const requestUserPermission = async () => { |
| 365 | + let isAuthorized = false; |
| 366 | + |
| 367 | + if (Platform.OS === 'android') { |
| 368 | + const androidAuthStatus = await PermissionsAndroid.request( |
| 369 | + PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS |
| 370 | + ); |
| 371 | + isAuthorized = androidAuthStatus === 'granted'; |
| 372 | + } else if (Platform.OS === 'ios') { |
| 373 | + const iOsAuthStatus = await messaging().requestPermission(); |
| 374 | + isAuthorized = |
| 375 | + iOsAuthStatus === messaging.AuthorizationStatus.AUTHORIZED || |
| 376 | + iOsAuthStatus === messaging.AuthorizationStatus.PROVISIONAL; |
| 377 | + } |
| 378 | + |
| 379 | + // refer the `fetchAndSetPushToken` method from the previous section for how to get and set the push token |
| 380 | + |
| 381 | + if (isAuthorized) { |
| 382 | + console.log('User has notification permissions enabled.'); |
| 383 | + } else { |
| 384 | + console.log('User has notification permissions disabled'); |
| 385 | + } |
| 386 | +}; |
| 387 | +``` |
| 388 | + |
| 389 | +Note that either one of the above approaches is sufficient to inform the Klaviyo SDK of the permission change. |
347 | 390 |
|
348 | 391 | ### Receiving Push Notifications
|
349 | 392 |
|
|
0 commit comments