Vanilla Integration

If you are using Expo into your application, you can jump directly to the Expo integration step.

Installation


Start installing the Batch React-Native plugin with the package manager of your choice:

yarn add @batch.com/react-native-plugin

Android extra steps


Starting the SDK

Update your MainApplication file as following:

MainApplication.kt
import com.batch.batch_rn.RNBatchModule

class MainApplication : Application(), ReactApplication {
    // ...
    override fun onCreate() {
        super.onCreate()
        // ...
        // Start Batch Plugin
        RNBatchModule.initialize(this)
    }
}

Configuring onNewIntent

Add the following in your MainActivity :

MainActivity.kt
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)
//}

Install dependencies

Setup the required dependencies in gradle scripts:

android/build.gradle
buildscript {
    ...
    dependencies {
        ...
        classpath 'com.google.gms:google-services:4.3.4'
    }
}
android/app/build.gradle
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 install

Start 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)
  }
 }

Enable Push Capabilities

Open the .xcworkspace in the ios folder. The in the project window:

  • Select your project in the sidebar

  • Go to Signing & Capabilities

  • Press on + Capability

  • Add Push Notifications

Setting up your APIKey


iOS

Edit your Info.plist and add the following:

Info.plist
<key>BatchAPIKey</key>
<string>YOUR_BATCH_API_KEY</string>

Android

Edit your android/app/build.gradle and add:

android/app/build.gradle
 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