Skip to content

Commit da50348

Browse files
github-actions[bot]evan-masseauajaysubrakennyklaviyodependabot[bot]
authored
Generated Docs for [CHNL-5370] Updating readme to include instructions on how to use initialize and setToken from RN code. (#113)
* Link to license file in README (#60) Co-authored-by: Evan Masseau <> * Updated readme to include troubleshooting steps when running pod install (#63) updated readme * Updating version to 0.1.1 (#66) * 0.1.1 * updating version * reverting back to use older rn version * updating yarn lock * removing changes to package lock file * Fixes for React Native Apps <= 0.72.0 (#71) * Fix the react-native gradle dependency * Add a local property to enable specifying react-native version for the SDK for local development * Specify Kotlin version that is compatible with Android SDK's (although this didn't seem to actually cause any build errors) * Remove accidental use of an extension method that is technically SDK 24+ --------- Co-authored-by: Evan Masseau <> * Update local.properties template files (#68) * Add template local.properties file to example app * Be clearer about VCS status of these files. --------- Co-authored-by: Evan Masseau <> * Bump example iOS project lockfile (#74) I think this needs to be bumped when we increase the overall project version. Co-authored-by: Evan Masseau <> * Github CI Updates (#75) * Move doc bot to its own file so we don't have to see it as skipped all the time. * Use same ruby version as local dev environment * Stop using `yarn pod-install` because it creates an erroneous xcode.env.local file that breaks the hermes build scripts. --------- Co-authored-by: Evan Masseau <> * Updating to iOS SDK 3.0.2 (#77) * 0.1.1 * updated to us latest iOS SDK * removing package lock * Pod install | static linking issue | RCT Folly | Turbo modules (#70) * 0.1.1 * removed turbo module stuff and added xcconfig * removing package lock * comitting example app pod lock file --------- Co-authored-by: Evan Masseau <> Co-authored-by: Evan C Masseau <[email protected]> * Readme rewrite with references to native SDKs and Klaviyo help center (#62) --------- Co-authored-by: Evan Masseau <> Co-authored-by: Kenny Tsui <[email protected]> * Release 0.1.2 (#84) * 0.1.1 * Release 0.1.2 * removed package lock * podlock file changes * Break up CI files so we can get individual badges (#86) * Break up CI files so we can get individual badges * Add badges and fix names --------- Co-authored-by: Evan Masseau <> * Add action to publish to NPM after publishing a release (#82) --------- Co-authored-by: Evan Masseau <> * Update README.md (#90) * Publish to NPM with the default npm registry configured in package.json (#92) --------- Co-authored-by: Evan Masseau <> * Updated iOS instructions in example app (#99) * 0.1.1 * udpate the example app to implement push * hanlded deep links * removed package json lock file * updated to addressed Evan comments in the pr * Fix mistaken duplicate open push section (#95) Co-authored-by: Evan Masseau <> * Remove local.properties from git index (#96) * remove local.properties from git index, changes were still being tracked despite gitignore * missed variable --------- Co-authored-by: Evan Masseau <> * Update README.md (#105) * Update README.md * Update README.md * Implemented deep links to call react native in example iOS app (#104) * 0.1.1 * implemented deep linking calls to react native * removed unused method * removed package lock * added deep linking hanlder * [CHNL-5592] Adding troubleshooting guide linked from readme (#109) * 0.1.1 * added troubleshooting * removed packagfe lock * added troibleshoting to the contents * swapped android and ios * [Dependabot] Bump ip from 1.1.8 to 1.1.9 (#111) Bumps [ip](https://github.com/indutny/node-ip) from 1.1.8 to 1.1.9. - [Commits](indutny/node-ip@v1.1.8...v1.1.9) --- updated-dependencies: - dependency-name: ip dependency-type: indirect ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Add example app instructions to README, make use of Gemfile (#97) * To make proper use of bundle, lockfile needs to be committed. I specified a cocoapods version since podfile.lock contains cocoapods version! Add explicit instructions about example app * Added alias for entire example app setup script Try updating CI step to use gem to pin cocoapods version --------- Co-authored-by: Evan Masseau <> * Bridge initialize and setPushToken to RN layer (#98) * Add an initialize method to react native layer * Add set push token to RN layer * Updated to latest versions of native SDKs * Elaboration on example app initialize/setPushToken, use a consistent value for example API key * SCREAMING_SNAKE_CASE --------- Co-authored-by: Evan Masseau <> * [CHNL-5370] Updating readme to include instructions on how to use `initialize` and `setToken` from RN code. (#113) * 0.1.1 * added troubleshooting * updated readme with initilize and set token instructions * adding notifee stuff to trouble shooting guide * removed duplicate android * removed package.json * some minor fixes * added example on requesting permission * Apply suggestions from code review Co-authored-by: Evan C Masseau <[email protected]> * minor updates * review comments * updated readme --------- Co-authored-by: Evan C Masseau <[email protected]> * Generated docs for bf5467b --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Evan C Masseau <[email protected]> Co-authored-by: Ajay Subramanya <[email protected]> Co-authored-by: Kenny Tsui <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions <[email protected]>
1 parent 8c12b00 commit da50348

15 files changed

+277
-214
lines changed

Diff for: README.md

+119-76
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- [Android](#android-1)
1818
- [iOS](#ios-1)
1919
- [Initialization](#initialization)
20+
- [React Native Initialization](#react-native-initialization)
21+
- [Native Initialization](#native-initialization)
2022
- [Identifying a Profile](#identifying-a-profile)
2123
- [Reset Profile](#reset-profile)
2224
- [Anonymous Tracking](#anonymous-tracking)
@@ -25,6 +27,8 @@
2527
- [Prerequisites](#prerequisites)
2628
- [Setup](#setup)
2729
- [Collecting Push Tokens](#collecting-push-tokens)
30+
- [React Native Token Collection](#react-native-token-collection)
31+
- [Native Token Collection](#native-token-collection)
2832
- [Receiving Push Notifications](#receiving-push-notifications)
2933
- [Rich Push](#rich-push)
3034
- [Tracking Open Events](#tracking-open-events)
@@ -86,9 +90,11 @@ yarn add klaviyo-react-native-sdk
8690
```
8791

8892
### Example App
93+
8994
We have included a bare-bones example app in this repository for reference of how to integrate with our SDK.
9095
It is primarily intended to give code samples such as how and where to `initialize` or how to implement notification
9196
delegate methods on iOS. To actually run the example app:
97+
9298
- Clone this repository
9399
- From the root directory, run `yarn example-setup`. This is an alias that will do the following:
94100
- Run `yarn install --immutable` from the root directory
@@ -129,85 +135,28 @@ Install [Cocoapods](https://cocoapods.org/) if you have not already.
129135

130136
## Initialization
131137

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.
179140

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.
182143

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.
188147

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
191149

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:
196151

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');
202155
```
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.
204156

205-
## Initialization
157+
### Native Initialization
206158

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
211160
[example app](./example) in this repository for guidance:
212161

213162
- [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:
333282

334283
### Collecting Push Tokens
335284

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:
337341

338342
- [Android](https://github.com/klaviyo/klaviyo-android-sdk#Collecting-Push-Tokens)
339343
- [iOS](https://github.com/klaviyo/klaviyo-swift-sdk#Collecting-Push-Tokens)
340344

341345
#### Notification Permission
342346

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.
347390

348391
### Receiving Push Notifications
349392

Diff for: Troubeshooting.md

+7
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,10 @@
3939

4040
2. If the command `pod install` is outputting version mismatch errors for `KlaviyoSwift`, please run `pod update KlaviyoSwift`
4141
as indicated in the error message to update your local pods spec repo.
42+
43+
### `UNUserNotificationCenter` delegate methods not being called
44+
45+
If you are not seeing the delegate methods for `UNUserNotificationCenter` being called in `AppDelegate`, there are two possible reasons for this,
46+
47+
1. [Notifee](https://notifee.app/) intercepts the AppDelegate delegate methods and hence you may not receive the delegate calls if notifee is included in the iOS project. The solution is to remove notifee dependency from your project or exclude it for iOS.
48+
2. Firebase iOS SDK also swizzles AppDelegate methods when configured on your iOS app. If after disabling notifee, if the delegates are still not called, this may be the reason. Method swizzling can be turned off by following [Firebase's documentation](https://firebase.google.com/docs/cloud-messaging/ios/client).

Diff for: docs/assets/highlight.css

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
--dark-hl-8: #4EC9B0;
2020
--light-hl-9: #098658;
2121
--dark-hl-9: #B5CEA8;
22+
--light-hl-10: #800000;
23+
--dark-hl-10: #808080;
24+
--light-hl-11: #800000;
25+
--dark-hl-11: #569CD6;
2226
--light-code-background: #FFFFFF;
2327
--dark-code-background: #1E1E1E;
2428
}
@@ -34,6 +38,8 @@
3438
--hl-7: var(--light-hl-7);
3539
--hl-8: var(--light-hl-8);
3640
--hl-9: var(--light-hl-9);
41+
--hl-10: var(--light-hl-10);
42+
--hl-11: var(--light-hl-11);
3743
--code-background: var(--light-code-background);
3844
} }
3945

@@ -48,6 +54,8 @@
4854
--hl-7: var(--dark-hl-7);
4955
--hl-8: var(--dark-hl-8);
5056
--hl-9: var(--dark-hl-9);
57+
--hl-10: var(--dark-hl-10);
58+
--hl-11: var(--dark-hl-11);
5159
--code-background: var(--dark-code-background);
5260
} }
5361

@@ -62,6 +70,8 @@
6270
--hl-7: var(--light-hl-7);
6371
--hl-8: var(--light-hl-8);
6472
--hl-9: var(--light-hl-9);
73+
--hl-10: var(--light-hl-10);
74+
--hl-11: var(--light-hl-11);
6575
--code-background: var(--light-code-background);
6676
}
6777

@@ -76,6 +86,8 @@
7686
--hl-7: var(--dark-hl-7);
7787
--hl-8: var(--dark-hl-8);
7888
--hl-9: var(--dark-hl-9);
89+
--hl-10: var(--dark-hl-10);
90+
--hl-11: var(--dark-hl-11);
7991
--code-background: var(--dark-code-background);
8092
}
8193

@@ -89,4 +101,6 @@
89101
.hl-7 { color: var(--hl-7); }
90102
.hl-8 { color: var(--hl-8); }
91103
.hl-9 { color: var(--hl-9); }
104+
.hl-10 { color: var(--hl-10); }
105+
.hl-11 { color: var(--hl-11); }
92106
pre, code { background: var(--code-background); }

0 commit comments

Comments
 (0)