File Based Initialization

Overview

To streamline the integration process and minimize initialization errors, MoEngage supports Script-Based Initialization. This approach allows you to manage App IDs and configuration settings directly within native configuration files, keeping them separate from your application logic.

This article outlines how you can use the form-based interface to generate a validated code snippet for initialization and access module-specific configurations.

info

Information

Alternatively, the SDK can be initialized manually. If you require this approach, please refer to the guide on Framework Initialization.

Script-based Initialization

Follow these steps to generate your initialization script:

  1. Navigate to the Initialization Website.
  2. Configure the values based on your application requirements. Refer to the Configuration Parameters tables below:
  3. Click Generate Code at the bottom of the form.

Android Configuration (XML)

For Android, initialization is handled by placing an XML configuration file in the application's resource directory.

Android Configuration Reference

Below is the comprehensive list of keys available for moengage.xml.

arrow_drop_down Android Configuration 

 

Category XML Key Name Type Description
Core com_moengage_core_workspace_id String Specifies your App ID. This field is mandatory.
  com_moengage_core_file_based_initialisation_enabled Boolean Set to true to enable this feature.
  com_moengage_core_data_center Integer Default: 1. For more info, refer Data Center values.
  com_moengage_core_environment String Supported values are: default, live, or test.
  com_moengage_core_custom_base_domain String Specifies the base custom proxy domain to route SDK network traffic through your own subdomain.
  com_moengage_core_integration_partner String Specifies the core integration partner. For example, segment or mparticle.
Push com_moengage_push_notification_small_icon Drawable Resource ID for small icon.
  com_moengage_push_notification_large_icon Drawable Resource ID for large icon.
  com_moengage_push_notification_color Color Notification accent color.
  com_moengage_push_notification_token_retry_interval Integer Retry interval (in seconds) for token registration.
  com_moengage_push_kit_registration_enabled Boolean If true, SDK registers for push token.
Logs com_moengage_core_log_level Integer 0 (No Log) to 5 (Verbose). Default: 3.
  com_moengage_core_log_enabled_for_release_build Boolean If true, prints logs in release builds.
Security com_moengage_core_storage_encryption_enabled Boolean Enables local storage encryption.
  com_moengage_core_network_encryption_enabled Boolean Enables payload encryption over the network.
Sync com_moengage_core_periodic_data_sync_enabled Boolean Enables periodic data sync in the foreground.
  com_moengage_core_background_data_sync_enabled Boolean Enables periodic data sync in the background.
In-App com_moengage_inapp_show_in_new_activity_enabled Boolean Required for specific TV/Android setups.
warning

Troubleshooting

If the XML file is missing or the com_moengage_core_workspace_id is empty, the SDK will throw a ConfigurationMismatchError.

Add Configuration File

  1. Create a file named moengage.xml.

    info

    Information

    • File-Based Init: Calling MoEInitializer.initializeDefaultInstance(context) instructs the SDK to look for and read the moengage.xml file.
    • Code-Based Init: Calling MoEInitializer.initialize(context, moEngage.Builder) will initialize the SDK using the configuration object passed in the parameters, ignoring the XML file even if it exists.
  2. Place it in android/app/src/main/res/values/.
  3. Add your configuration file generated in the Initialization Website.
XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Optional: Enable File Based Init -->
    <bool name="com_moengage_core_file_based_initialisation_enabled">true</bool>
    
    <!-- Mandatory: Your App ID -->
    <string name="com_moengage_core_workspace_id">YOUR_WORKSPACE_ID</string>
    
    <!-- Mandatory: Data Center (Default 1) -->
    <integer name="com_moengage_core_data_center">1</integer>

    <!-- Optional: Custom Proxy Domain -->
    <string name="com_moengage_core_custom_base_domain">data.example.com</string>
    
    <!-- Optional: Notification Settings -->
    <drawable name="com_moengage_push_notification_small_icon">@drawable/ic_notification</drawable>
    <color name="com_moengage_push_notification_color">#FF0000</color>

    <!-- Optional: Logging (0=NoLog, 5=Verbose) -->
    <integer name="com_moengage_core_log_level">3</integer>
</resources>

 

iOS Configuration (Info.plist)

For iOS, initialization is handled by adding a configuration dictionary to your Info.plist.

iOS Configuration Reference

Below is the comprehensive list of keys available for the MoEngage dictionary.

arrow_drop_down iOS Configurations

 

Category Plist Key Type Description
Core WorkspaceId String Specifies your App ID. It is a Mandatory field.
  IsSdkAutoInitialisationEnabled Boolean Set to true to enable SDK auto initialisation.
  DataCenter Integer

Specifies the Data Center value. This is a Mandatory field. The default value is 1. For more info, refer to Data Center values.

  IsTestEnvironment String / Boolean Customer selected option (true/false).
Default value is:  $(SWIFT_ACTIVE_COMPILATION_CONDITIONS).
  CustomBaseDomain String Specifies the base custom proxy domain to route SDK network traffic through your own subdomain.
  IntegrationPartner String Specifies your integration partner. For example, segment or mparticle.Default value: none.
  AppGroupName String Specifies the App Group name used for sharing SDK data. Default value: "".
Logs IsLoggingEnabled Boolean Set to true to enable SDK logs.
  Loglevel Integer 0 to 5. Default: 2.
Security IsStorageEncryptionEnabled Boolean Enables local storage encryption. Default value: false.
  KeychainGroupName String Specifies the keychain group name used for storing encryption keys. This is a mandatory field if IsStorageEncryptionEnabled is true. Default value: "".
  IsNetworkEncryptionEnabled Boolean Enables payload encryption. Default: false.
  EncryptionEncodedTestKey String Dashboard auto-populated string. Used if IsNetworkEncryptionEnabled is true.
  EncryptionEncodedLiveKey String Dashboard auto-populated string. Used if IsNetworkEncryptionEnabled is true.
Sync AnalyticsEnablePeriodicFlush Boolean Enables periodic data flush. Default: true.
  AnalyticsPeriodicFlushDuration Integer Flush interval in seconds. Default: 60.
In-App InAppDisplaySafeAreaInset Real Decimal value representing safe area padding. Default: 0.
  InAppShouldProvideDeeplinkCallback Boolean If true, provides callback on deeplink. Default: false.

 

Data Center Values

Configure the integer corresponding to your region. Incorrect values will result in data loss. 

Data Center Dashboard host
1 dashboard-01.moengage.com
2 dashboard-02.moengage.com
3 dashboard-03.moengage.com
4 dashboard-04.moengage.com
5 dashboard-05.moengage.com
6 dashboard-06.moengage.com

Update Info.plist

  1. Open your project's Info.plist (found in ios/ProjectName/).
  2. Create a new Top-Level Key named MoEngage of type Dictionary.
  3. Add the configuration file generated in the Initialization Website.
warning

Warning

The key IsSdkAutoInitialisationEnabled uses the British spelling ('s'). Ensure you use the exact key name shown below, or initialization will fail.

XML Snippet Representation:

XML
<key>MoEngage</key>
<dict>
    <key>WorkspaceId</key>
    <string>YOUR_WORKSPACE_ID</string>
    <key>IsTestEnvironment</key>
    <string>$(SWIFT_ACTIVE_COMPILATION_CONDITIONS)|$(GCC_PREPROCESSOR_DEFINITIONS)</string>
    <key>DataCenter</key>
    <integer>1</integer>
    <key>CustomBaseDomain</key>
    <string>data.example.com</string>
    <key>IsLoggingEnabled</key>
    <true/>
</dict>

Framework Level Initialization

After you configure the native files, the initialization code in your hybrid framework is simplified.

Android Native Setup

Before initializing the SDK in JavaScript, you must initialize the native module in your Android Application class. Add the following code to android/app/src/main/java/com/your_app/MainApplication.java (or .kt) inside the onCreate() method.

JavaKotlin
import com.moengage.react.MoEInitializer;

@Override
public void onCreate() {
  super.onCreate();
  // ... existing code
  MoEInitializer.INSTANCE.initializeDefaultInstance(this);
}

iOS Native Setup

Before initializing the SDK in JavaScript, you must initialize the native module in your iOS AppDelegate class. Add the following code to didFinishLaunchingWithOptions method.

Objective-CSwift
#import <MoEngageSDK/MoEngageSDK.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // ... existing code
  [[MoEngageInitializer sharedInstance] initializeDefaultInstanceWithAdditionalReactConfig:[[MoEngageReactSDKDefaultInitializationConfig alloc] init]];
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

Initialize React-Native Component

Initialize the MoEngage Plugin in the App.js or App.ts of your application once the component is mounted.

TypeScript
import ReactMoE from 'react-native-moengage';
ReactMoE.initialize("YOUR_Workspace_ID");

Example

TypeScript
import ReactMoE from 'react-native-moengage';

useEffect(() => {
    ReactMoE.initialize("YOUR_Workspace_ID");
}, []);
info

Note

If you have a class-based component then you can initialize in the render() or componentDidMount()

Initialize with Configuration (optional)

TypeScript
import ReactMoE from 'react-native-moengage';
import { MoEInitConfig, MoEPushConfig, MoEngageLogConfig, MoEngageLogLevel } from "react-native-moengage";
const moEInitConfig = new MoEInitConfig(
  MoEPushConfig.defaultConfig(),
  new MoEngageLogConfig(MoEngageLogLevel.DEBUG, isEnabledForReleaseBuild)
);
ReactMoE.initialize(YOUR Workspace ID, moEInitConfig);
warning

Warning

Make sure you are setting the Push/InApp callback listeners before calling the initialize().

Migration and Precedence

Android Migration Steps

To migrate from manual code-based initialization to the XML file-based approach, follow these steps:

  1. Add Configuration File: Place the moengage.xml config file in the correct path (for example, src/main/assets/moengage.xml).
  2. Update Application Class: Remove the existing initialization code (the manual MoEngage.Builder logic) from your Application class and replace it with the default instance initialization to enable reading from the XML file.
Java Kotlin
import com.moengage.react.MoEInitializer;
MoEInitializer.INSTANCE.initializeDefaultInstance(this);

iOS Migration Steps

To migrate from code-based initialization to the Info.plist based approach, follow these steps:

  1. Update Info.plist: Add the required MoEngage configuration keys (for example, WorkspaceId, DataCenter, etc.) directly to your Info.plist file.
  2. Update AppDelegate: Remove the existing initialization code (the manual MoEngageSDKConfig logic) from your AppDelegate class and replace it with the default instance initialization to enable reading from the Info.plist file.
Objective-C Swift
#import <MoEngageSDK/MoEngageSDK.h>

[[MoEngageInitializer sharedInstance] initializeDefaultInstanceWithAdditionalReactConfig:[[MoEngageReactSDKDefaultInitializationConfig alloc] init]];

Precedence Rules

The source of configuration is determined by the initialization function called in your native code:

  • Android:
    • File-Based Init: Calling MoEInitializer.initializeDefaultInstance(context) instructs the SDK to look for and read the moengage.xml file.
    • Code-Based Init: Calling MoEInitializer.initialize(context, moEngage.Builder) will initialize the SDK using the configuration object passed in the parameters, ignoring the XML file even if it exists.
  • iOS: Auto-initialization (via Info.plist) occurs first. However, if you subsequently call the manual initialize method with a configuration object in your code, it will update the current instance configuration.

Environments (Test vs. Live)

You can configure Test/Live environments within these files.

  • Android: Use the key <string name="com_moengage_core_environment">test</string>.
  • iOS: Use <key>IsTestEnvironment</key> <true/>

Previous

Next

Was this article helpful?
0 out of 0 found this helpful

How can we improve this article?