Step 2: SDK Initialization
Overview
You must initialize the IM SDK before using its features.
In most scenarios, you need to initialize the IM SDK only once during the application lifecycle.
Initialization
You can initialize the SDK in the following steps:
1. Prepare an
SDKAppID
.2. Set the
SdkConfig
.3. Set the SDK event listener.
4. Call
Init
to initialize the SDK.The detailed steps are as follows.
Preparing an SDKAppID
To perform the initialization, you must have a correct
SDKAppID
.
SDKAppID
is the unique ID that the IM service uses to identify a customer account. We recommend you apply for a new SDKAppID
for every independent app to automatically isolate messages between SDKAppIDs
.
You can view all SDKAppIDs
in the IM console or click Create Application to create an SDKAppID
.
Setting the SdkConfig
Before initializing the SDK, you need to initialize the SdkConfig object, which is used to set the local SDK cache and log position.
It is used to configure the storage path of IM running logs and data.
sdk_config_config_file_path
Storage path of local IM data.
Caution
The app needs to have read-write access to this path.
sdk_config_log_file_path
It is the storage path of the IM logs.
Caution
The app needs to have read-write access to this path.
Calling the initialization API
Sample code:
namespace Com.Tencent.IM.Unity.UIKit{public static void Init() {string sdkappid = ""; // Get the `SDKAppID` from the IM consoleSdkConfig sdkConfig = new SdkConfig();sdkConfig.sdk_config_config_file_path = Application.persistentDataPath + "/TIM-Config";sdkConfig.sdk_config_log_file_path = Application.persistentDataPath + "/TIM-Log";TIMResult res = TencentIMSDK.Init(long.Parse(sdkappid), sdkConfig);}}
Registering a SDK global event listener
After the initialization, SDK will throw such events as connection status and login ticket expiration through the
NetworkStatusListenerCallback
, UserSigExpiredCallback
, and other callbacks.
We recommend you register a global event listener immediately after calling initSDK
and perform logic processing in such callbacks.The callbacks are as described below:
Event Callback | Description |
Callback for receiving a new message | |
Callback for a message read receipt | |
Callback for a message recall | |
Callback for the upload progress of a message element | |
Callback for a group system message | |
Callback for a group attribute change | |
Callback for a change in the unread message count of a conversation | |
Callback for listening for the network connection status | |
Callback for being kicked offline | |
Callback for ticket expiration | |
Callback for adding a friend | |
Callback for deleting a friend | |
Callback for updating the profile of a friend | |
Callback for a friend request | |
Callback for deleting a friend request | |
Callback for reading a friend request | |
Callback for adding a friend to the blocklist | |
Callback for deleting a friend from the blocklist | |
Log callback | |
Callback for a message update | |
Callback for getting the list of group members who have read a group message |
Caution
If you receive the
UserSigExpiredCallback
callback, the UserSig
that you use for login has expired. In this case, you need to use the newly issued UserSig
to log in again. If you continue to use the expired UserSig
, the IM SDK will enter an infinite login loop.// Uninitialization
Generally, if your application's lifecycle is the same as the IM SDK's lifecycle, you don't need to uninitialize the IM SDK before exiting the application.
However, you can uninitialize the IM SDK in special cases, for example, only after you enter a specific UI and no longer use it after exiting the UI.
Sample code:
// Uninitialize the SDKTencentIMSDK.Uninit();
Others
It is used to display the result returned when the SDK is called. When
res
is TIMResult.TIM_SUCC = 0
, the API call is successful.After the SDK is successfully initialized, add required listeners to avoid missing messages.
FAQs