React Native Integration
Contents
Our React Native plugin leverages our native SDKs for iOS and Android. This guide assumes you are adding Mobile to an existing React Native project. If you are starting from scratch, follow the Getting Started tutorial from React Native.
Add and Link the Plugin
First, add the plugin to your project via NPM, then navigate to its folder.npm i react-native-sailthru-mobile
cd node_modules/react-native-sailthru-mobile
iOS Integration
Open your Xcode project or workspace. Drag into Libraries the following files fromnode_modules/react-native-sailthru-mobile
:
RNSailthruMobile.h
RNSailthruMobile.m
RNSailthruMobileBridge.h
RNSailthruMobileBridge.m
RNSailthruMobile.m
and RNSailthruMobileBridge.m
. Make sure these files' membership is your main app's target by selecting your project name from the right hand menu's Target Membership section.
Now, link the plugin with the native SDK by adding the iOS SDK to your project. We suggest you use CocoaPods to easily integrate the SDK to your project, but you can also install the framework manually (SailthruMobile.framework can be obtained from node_modules/react-native-sailthru-mobile).
Import the RNSailthruMobileBridge header file #import "RNSailthruMobileBridge.h"
into the native UIApplicationDelegate implementation (or the Swift bridging header). You will then need replace the code that creates your RCTRootView with the code below. This adds the Sailthru Mobile React Native modules to the root view.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... id<RCTBridgeDelegate> moduleInitialiser = [[RNSailthruMobileBridge alloc] initWithJSCodeLocation:jsCodeLocation appKey:SDK_KEY]; // Obtain SDK key from your Sailthru Mobile app settings RCTBridge * bridge = [[RCTBridge alloc] initWithDelegate:moduleInitialiser launchOptions:launchOptions]; RCTRootView * rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"YOUR_MODULE_NAME" initialProperties:nil]; ... }This completes the plugin integration. If you haven't already done so, make sure you setup your app for push notifications in your Xcode project and you upload the push certificates in your app's dashboard.
Additional Options
There are additional options that can be specified on the RNSailthruMobileBridge object:RNSailthruMobileBridge *sailthruMobileBridge = [[RNSailthruMobileBridge alloc] initWithJSCodeLocation:jsCodeLocation appKey:SDK_KEY pushAuthorizationOption: STMPushAuthorizationOptionNoRequest geoIpTrackingDefault:NO]; sailthruMobileBridge.displayInAppNotifications = NO;These can be used to override whether the SDK should:
- request push authorization automatically
- enable geo IP tracking by default
- display in app notifications
Android Integration
Add the following to your project's gradle file (android/settings.gradle
):
include ':react-native-sailthru-mobile' project(':react-native-sailthru-mobile').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sailthru-mobile/android')Add the following to your app's gradle file (
android/app/build.gradle
):
repositories { google() maven { url "https://github.com/carnivalmobile/maven-repository/raw/master/" } } dependencies { implementation project(':react-native-sailthru-mobile') }Next, you need to link the package in your
MainApplication.java
.
// Add this import line import com.reactlibrary.RNSailthruMobilePackage; public class MainApplication extends Application implements ReactApplication { // ... protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new RNSailthruMobilePackage(getApplicationContext(), SDK_KEY, true) // Obtain SDK key from your Sailthru Mobile app settings ); } // ... }This completes the plugin integration. If you haven't already done so, make sure you setup your app for push notifications in your Android Studio project you have the right FCM details in your app's dashboard.
Additional Options
There are additional options that can be specified on the RNSailthruMobilePackage object:// Add this import line import com.reactlibrary.RNSailthruMobilePackage; public class MainApplication extends Application implements ReactApplication { // ... protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), RNSailthruMobilePackage.Builder.createInstance(getApplicationContext(), SDK_KEY) .setGeoIPTrackingDefault(false) .setDisplayInAppNotifications(false) .build() ); } // ... }These can be accessed by using the RNSailthruMobilePackage Builder and can override whether the SDK should:
- enable geo IP tracking by default
- display in app notifications