All Products
Search
Document Center

Mobile Platform as a Service:Android

Last Updated:Sep 18, 2023

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:

  1. Add SDK

  2. Use SDK

Prerequisites

You have integrated mPaaS to your project.

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 method. This API is available only in baseline 10.1.60 and later versions.

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 callback to receive business data. If this API is called, the client SDK will call the syncCallback class after receiving synchronized data.

unregisterBiz(String bizType)

Unregisters a specified synchronization configuration. If this API is called, the client SDK will not call the syncCallback class when receiving synchronized data.

reportMsgReceived(SyncMessage syncMessag)

After the data is received in the syncCallback implementation class, this API is called to notify MSS that the sync data has been received. Before receiving the reportMsgReceived message, MSS attempts to resend the data for a maximum of six times. If all resending attempts fail, the data will be permanently deleted.

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);
    }
    }

Follow-up steps