Vanilla Integration
Installation
Start installing the Batch React-Native plugin with the package manager of your choice:
yarn add @batch.com/react-native-pluginnpm install @batch.com/react-native-pluginAndroid extra steps
Starting the SDK
Update your MainApplication file as following:
import com.batch.batch_rn.RNBatchModule
class MainApplication : Application(), ReactApplication {
// ...
override fun onCreate() {
super.onCreate()
// ...
// Start Batch Plugin
RNBatchModule.initialize(this)
}
}import com.batch.batch_rn.RNBatchModule
public class MainApplication extends Application implements ReactApplication {
// ...
@Override
public void onCreate() {
super.onCreate();
// ...
// Start Batch Plugin
RNBatchModule.initialize(this);
}
}Configuring onNewIntent
Add the following in your MainActivity :
import android.content.Intent
import com.batch.android.Batch
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Batch.onNewIntent(this, intent)
}
// Or, if your project is using AndroidX Activity 1.9+
//override fun onNewIntent(intent: Intent) {
// super.onNewIntent(intent)
// Batch.onNewIntent(this, intent)
//}import android.content.Intent;
import com.batch.android.Batch;
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Batch.onNewIntent(this, intent);
}Install dependencies
Setup the required dependencies in gradle scripts:
buildscript {
...
dependencies {
...
classpath 'com.google.gms:google-services:4.3.4'
}
}dependencies {
implementation platform('com.google.firebase:firebase-bom:28.0.0') // needed if you don't have @react-native-firebase/app
implementation "com.google.firebase:firebase-messaging" // needed if you don't have @react-native-firebase/messaging
...
}
apply plugin: 'com.google.gms.google-services'Firebase config
Add your google-services.json file to android/app.
Small push notification icon
Follow the Customizing Batch notifications guide to display your notification icon correctly on Android.
iOS extra steps
Install dependencies
As Batch React-Native plugin integrate the iOS Batch SDK, you have to install native dependencies.
cd ios && pod installStart the SDK
Add the following in your AppDelegate:
import RNBatchPush
@main
class AppDelegate: RCTAppDelegate {
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
...
/// Start Batch Plugin
RNBatch.start()
...
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}#import <RNBatchPush/RNBatch.h>
// or, use #import "RNBatch.h" if the framework import didn't work
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
/// Start Batch Plugin
[RNBatch start];
...
return YES;
}Enable Push Capabilities
Open the .xcworkspace in the ios folder. The in the project window:
Select your project in the sidebar
Go to
Signing & CapabilitiesPress on
+ CapabilityAdd
Push Notifications
Setting up your APIKey
iOS
Edit your Info.plist and add the following:
<key>BatchAPIKey</key>
<string>YOUR_BATCH_API_KEY</string>Android
Edit your android/app/build.gradle and add:
defaultConfig {
// ...
resValue("string", "BATCH_API_KEY", "YOUR_BATCH_API_KEY")
}defaultConfig {
// ...
resValue "string", "BATCH_API_KEY", "YOUR_BATCH_API_KEY"
}YOUR_BATCH_API_KEY is your SDK API Key. You'll find it in Settings → General
Enable push notifications
Add the following in your app code, ideally the first view a user sees when opening the app:
import { BatchPush } from '@batch.com/react-native-plugin'
// Ask for the permission to display notifications
// The push token will automatically be fetched by the SDK
BatchPush.requestNotificationAuthorization()
// iOS ONLY:
// If you are using Batch plugin < 7.0.0 please use the following method or update the plugin.
// BatchPush.registerForRemoteNotifications();
// Alternatively, you can call requestNotificationAuthorization later
// But, you should always refresh your token on each application start
// This will make sure that even if your user's token changes, you still get notifications
// BatchPush.refreshToken();Last updated

