Skip to main content

Initialization

Before using other features of the SDK, initialization must be performed first. This article details the initialization methods for the IMKit SDK.

For first-time users of RC, we recommend reading IMKit SDK Quick Start to complete developer account registration and other preparatory work.

Push Notification

Push notification is a common basic feature. The IMKit SDK has integrated RC's native push and multiple third-party push services, which are triggered after SDK initialization. Therefore, client-side push configurations must be provided before initialization. For details, refer to Enable Push Notifications.

Global Data Center

  • If you use the global data center with development version (dev) SDK 5.4.2 or later, ensure the correct AreaCode is passed in the initialization configuration.
  • If you use the global data center with stable version (stable) SDK or development version (dev) SDK earlier than 5.4.2, you must modify the IMLib SDK's connection service address to the global data center address before initialization. Otherwise, the SDK defaults to connecting to the China (Beijing) Data Center. For details, refer to Configure Global Data Center Service Address.

Process

tip

Starting from version 5.3.0, the IMLib SDK supports single-process mode.

The SDK supports both multi-process and single-process mechanisms.

If the SDK version is < 5.3.3 or ≥ 5.3.5, the multi-process mechanism is enabled by default. After initialization, the application launches the following processes:

  1. The main process of the application
  2. <application package name>:ipc: This is the core process for IM communication, isolated from the main process tasks.
  3. io.rong.push: RC's default push process. Whether this process starts depends on the push channel's activation strategy. For details, refer to Enable Push Notifications.

If the SDK version is ≥ 5.3.3 and < 5.3.5, the single-process mechanism is enabled by default.

Starting from version 5.3.0, you can enable or disable single-process mode before initialization using the enableSingleProcess interface of RongCoreClient. Note that enabling single-process mode will consume additional memory in the main process.

//Enable single-process mode
RongCoreClient.getInstance().enableSingleProcess(true);

Initialize the SDK

tip

The following initialization method requires SDK version ≥ 5.4.2. You can check the latest development (Dev) and stable (Stable) versions on the RC Official SDK Download Page.

Initialize the SDK in the onCreate() method of the Application class, passing the App Key for either the production or development environment.

String appKey = "YourAppKey"; // example: bos9p5rlcm2ba
InitOption initOption = new InitOption.Builder().build();

IMCenter.init(this, appKey, initOption);

The initialization configuration (InitOption) includes the AreaCode setting. The SDK will use the area code to obtain valid navigation service addresses, file service addresses, data statistics service addresses, and log service addresses.

  • If the App Key belongs to the China (Beijing) Data Center, no additional configuration is required—the SDK uses default settings.
  • If the App Key belongs to a global data center, you must provide a valid AreaCode configuration. Verify the App Key's data center in the Console and match it with the corresponding enum value in AreaCode.

For example, an App Key for the Singapore data center in production or development environment:

String appKey = "Singapore_dev_AppKey";
AreaCode areaCode = AreaCode.SG;

InitOption initOption = new InitOption.Builder()
.setAreaCode(areaCode)
.build();

IMCenter.init(context, appKey, initOption);

Besides the area code, the initialization configuration (InitOption) also includes:

  • Push notification toggle (enablePush): Whether to disable push notifications globally.
  • Main process toggle (isMainProcess): Whether the current process is the main process. By default, the SDK determines this automatically.
  • Navigation service address (naviServer): Generally not recommended for manual configuration. The SDK defaults to addresses corresponding to the area code.
  • File service address (fileServer): For private cloud use only.
  • Data statistics service address (statisticServer): Generally not recommended for manual configuration. The SDK defaults to addresses corresponding to the area code.
String appKey = "Your_AppKey";
AreaCode areaCode = AreaCode.BJ;

InitOption initOption = new InitOption.Builder()
.setAreaCode(areaCode)
.enablePush(true)
.setFileServer("http(s)://fileServer")
.setNaviServer("http(s)://naviServer")
.setStatisticServer("http(s)://StatisticServer")
.build();

IMCenter.init(context, appKey, initOption);

Initialize the SDK (Legacy Method)

tip

If your development version SDK is earlier than 5.4.2 or your stable version SDK is 5.3.8 or earlier, use the following initialization method. We recommend upgrading to the latest SDK version.

Initialize the SDK in the onCreate() method of the Application class, passing the App Key for either the production or development environment. You can use the initialization methods from RongCoreClient or RongIMClient.

public class App extends Application {
@Override
public void onCreate() {
...
String appKey = "YourAppKey";
Boolean enablePush = true;
RongIM.init(this, appKey, enablePush);
}
}
ParameterTypeDescription
contextContextApplication context.
appKeyStringThe development or production environment AppKey. Do not mix them. Optional if configured in AndroidManifest.xml.
enablePushbooleanWhether to Enable Push Notifications. Default is true. First-time settings take effect. If enablePush is already set to true and the push token is successfully reported, subsequent changes to false will not take effect.

For backward compatibility, IMLib SDK 5x still supports configuring the App Key in AndroidManifest.xml. However, due to App Key security concerns, RC no longer recommends this approach.

You can delay initialization until the user accepts the privacy agreement.

/**
* During app launch, check if the user has accepted the privacy agreement. If yes, initialize normally; otherwise, redirect to the privacy authorization page.
*/
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();

//Pseudo-code: Read privacy agreement acceptance status from SharedPreferences
boolean isPrivacyAccepted = getPrivacyStateFromSp();
//If accepted, initialize
if (isPrivacyAccepted) {
String appKey = "Your AppKey from RC Console";
//The first parameter must be the application context
RongIM.init(this.getApplicationContext(), appKey);
} else {
//If not accepted, redirect to the privacy authorization page
goToPrivacyActivity();
}
...
}
}

/**
* This class demonstrates how to initialize IM after the user accepts the privacy agreement.
*/
public class PrivacyActivity extends Activity implements View.OnClickListener {
...
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.accept_privacy:
//Pseudo-code: Save acceptance status to SharedPreferences
savePrivacyStateToSp();

String appKey = "Your AppKey from RC Console";
//The first parameter must be the application context
RongIM.init(this.getApplicationContext(), appKey);
break;
default:
...
}
}
}