The application which uses the latest Skylink Message Cache SDK for Android demonstrates the use of persistent message caching feature.
The architecture of the app is the way we organize the code to have a clear structure. We try to separate between the application layer and the SDK usage layer.
With the separated parts, the user can easily change each part without changing the others and extend the functionality of the application. For example, the user can using different view components to display GUI of the application while keeping the same logics of using the SDK.
The MVP (Model - View - Presenter) architecture used in the app mainly divided into three main parts : View (UI) - Presenter - Service
ChatUI
: Responsible for displaying GUI and getting user events.ChatPresenter
: Responsible for communicating with the CharService to get the job done. Acts as the interface between the view and CharService.ChatService
: Responsible for communicating with Skylink SDKs.
Implementation of the app is divided in to 2 Gradle modules.
app
: Implements the UI and app logicskylink_sample
: Contains Java class implementations taken from skylink-android-sample project.
ChatService implementation is completely taken from the skylink-android-sample project and modified slightly to enable message caching feature.
Clone this repository.
Import the project into Android Studio with File -> Open and select the project.
Follow the instructions here to create an App and a key on the Temasys Console.
Create a copy of config_example.xml
under skylink_sample/src/main/res/values
and name it config.xml
(OR any other name).
You may also choose to not create a new file and edit config_example.xml
itself as needed.
Add your preferred values for app_key
and app_secret
. An appropriate App key and corresponding secret are required to connect to Skylink successfully.
Instructions for populating the config.xml file can be found in skylink-android-sample/README.md.
Build the project.
Run the app module.
dependencies {
implementation(group: 'sg.com.temasys.skylink.sdk',
name: 'skylink_sdk',
version: '2.3.1-RELEASE',
ext: 'aar') {
transitive = true
exclude group: 'sg.com.temasys.skylink.sdk', module: 'skylink_message_cache_sdk'
}
implementation('sg.com.temasys.skylink.sdk:skylink_message_cache_sdk:1.0.1-RELEASE')
}
skylink_sample/src/main/java/sg/com/temasys/skylink/sdk/sampleapp/service/ChatService.java#Line:126
SkylinkConfig skylinkConfig = new SkylinkConfig();
// Enable message caching (Message caching is disabled by default in SkylinkConfig)
skylinkConfig.setMessageCachingEnable(true);
// Set maximum number of messages that will be cached per Skylink Room (default value is 50)
skylinkConfig.setMessageCachingLimit(100);
app/src/main/java/sg/com/temasys/skylink/sdk/messagecache/demo/ChatPresenter.java#Line:121
if ( SkylinkMessageCache.getInstance().isEnabled() ) {
// Get a readable session from the message cache to your room
JSONArray cachedMsgs = SkylinkMessageCache.getInstance().getReadableSession("<your-room-name>").getCachedMessages();
// Processing cached messages :-
// Assuming all cached messages are String messages (not JSONObject or JSONArray messages)
for (int i = 0; i < cachedMsgs.length(); i++) {
JSONObject cachedMsg = (JSONObject) cachedMsgs.get(i);
String senderId = cachedMsg.getString("senderId");
String msgContent = cachedMsg.getString("data");
long timestamp = cachedMsg.getLong("timeStamp");
}
}
// Get a writable session from the message cache to your room and clear cached messages of that room
SkylinkMessageCache.getInstance().getWritableSession("<your-room-name>").clearCachedMessages();
// Clear a specific cached room (including cached room information + cached messages)
SkylinkMessageCache.getInstance().clearRoom("<your-room-name>");
// Clear everything in the message cache (all cached rooms and cached messages)
SkylinkMessageCache.getInstance().clearAll();