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.
As Batch React-Native plugin integrate the iOS Batch SDK, you have to install native dependencies.
cd ios && pod install
By default, React-Native links libraries statically, but if you do it dynamically with use_framework!, please add the following to your Podfile:
Podfile
config = use_native_modules!
# From here
$static_framework = ['Batch']
pre_install do |installer|
Pod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {}
installer.pod_targets.each do |pod|
if $static_framework.include?(pod.name)
def pod.build_type;
Pod::BuildType.static_library
end
end
end
end
# To here
use_react_native!(
:path => config["reactNativePath"],
:hermes_enabled => true
)
Enable Push Capabilities
Open the .xcworkspace in the ios folder. The in the project window:
#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;
}
Since react-native 0.77, the AppDelegate community template has been migrated to Swift. If you have migrated to Swift or are using a new project, you need to add a xxx-Bridging-Header.h file to your project (Xcode will ask you to generate this for you if you create a dummy objective-c file and then delete it.) with #import <RNBatchPush/RNBatch.h> inside.
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();