This topic briefly describes how to fast integrate MSS to the Android client. You can access MAS through Native AAR or Portal & Bundle.
Important: Since June 28, 2020, mPaaS has stopped support for the baseline 10.1.32. Please use 10.1.68 or 10.1.60 instead. For how to upgrade the baseline from version 10.1.32 to 10.1.68 or 10.1.60, see mPaaS 10.1.68 upgrade guide or mPaaS 10.1.60 upgrade guide. |
The complete access process mainly includes the following 2 steps:
Prerequisites
You have integrated mPaaS to your project.
If you access MSS through Native AAR, ensure that you have added mPaaS to project.
If you access MSS in componentized access mode (through Portal & Bundle projects), ensure that you have completed the componentized access process.
Add SDK
Native AAR mode
Follow the instructions in AAR component management to install the SYNC component in the project through Component management (AAR).
Componentized access mode
Install the SYNC component in the Portal and Bundle projects through Component management (AAR).
For more information, see Manage component dependencies > Add/delete component dependencies.
Use SDK
In baseline 10.1.32 or later version, the MPSync
class at the mPaaS middle layer encapsulates all APIs of MSS. You can have a quick glance of these APIs in the following table. For more information about the APIs, see Android API reference.
API | Description |
setup(Application application) | Initializes basic services on which MSS depends. Call this API before you call the |
initialize(Context context) | Initializes APIs and MSS. |
appToForeground() | Notifies the client SDK that the App has been switched to the foreground and it needs to connect to the server. Call this API every time the App is switched to the foreground. |
appToBackground() | Notifies the client SDK that the 128344 has been switched to the background and it needs to disconnect from the server. Call this API every time the App is switched to the background. |
updateUserInfo(String sessionId) | Call this API when the login information (userId or sessionId) is changed. This API is called at least once. |
clearUserInfo() | Clears user information when a user logs out. |
registerBiz(String bizType, ISyncCallback syncCallback) | You can call this API to register a |
unregisterBiz(String bizType) | Unregisters a specified synchronization configuration. If this API is called, the client SDK will not call the |
reportMsgReceived(SyncMessage syncMessag) | After the data is received in the |
isConnected() | Checks whether MSS is running properly. |
Code sample
This sample is based on the mPaaS SDK 10.1.32 baseline. The example App provides a button. When a user taps this button, MSS obtains the device ID, and pushes the sync data to the target device specified in the console based on the device ID. In this sample, the sync ID is bizType
.
Note: This sample is only intended for demonstrating how to call MSS APIs, and is not the best practice of MSS. You can get the best practice code of MSS from Obtain code samples.
public void button1Clicked(View view)
{
// Obtain the device ID using the getUtdid method.
String utdid =UTDevice.getUtdid(MainActivity.this);
// Print mobile sync data in Logcat.
Log.e("=========",utdid);
// Initialize the API and MSS.
MPSync.initialize(MainActivity.this);
// Register a callback for receiving service data. If this API is called, the client SDK will call the syncCallback class after receiving synchronized data.
MPSync.registerBiz("bizType",new SyncCallBackImpl());
// Set up a network connection with the server.
MPSync.appToForeground();
}
public class SyncCallBackImpl implements ISyncCallback
{
@Override
public void onReceiveMessage(SyncMessage syncMessage) {
//Print mobile sync data in Logcat.
Log.e("=========",syncMessage.msgData);
// Notify the MSS server that the sync data has been received.
MPSync.reportMsgReceived(syncMessage);
}
}