All Products
Search
Document Center

Mobile Platform as a Service:Integrate service with Java SDK

Last Updated:Sep 18, 2023

This topic describes how to access the data synchronization service on the server by using Java SDK.

Import JAR package

After completing the Maven configuration, introduce the following dependencies in the master pom.xml file.

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-mpaas</artifactId>
    <version>1.0.0.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <optional>true</optional>
  <version>[4.3.2,5.0.0)</version>
</dependency>

Environment Variable Configuration

Configure environment variable MPAAS_AK_ENV and MPAAS_SK_ENV.

  • Linux and macOS system configuration methods execute the following commands:

    export MPAAS_AK_ENV=<access_key_id>
    export MPAAS_SK_ENV=<access_key_secret>
    Note

    access_key_id is replaced with the prepared AccessKey ID, and access_key_secret is replaced with the AccessKey Secret.

  • Windows system configuration method

    1. Create a new environment variable, add environment variables MPAAS_AK_ENV and MPAAS_SK_ENV, and write the prepared AccessKey ID and AccessKey Secret.

    2. Restart Windows system.

API description

Single data synchronization interface

The single data synchronization interface is used to synchronize data to a specified user or device.

Parameters

Business parameters are as follows:

Parameter

Data type

Required

Example

Description

appId

String

Required

ONEX570DA892117

Get App ID from the mPaaS console.

workspaceId

String

Required

PROD

Get Workspace ID from the mPaaS console.

bizType

String

Required

UCHAT

The synchronization identifier configured in the mPaaS console. See Console introduction for more details.

linkToken

String

Required

Push target ID. Enter the user ID if the push is based on users. Enter the device ID if the push is based on devices.

payload

String

Required

testpayload

Actual business message body in custom format, no more than 4,096 characters in length.

thirdMsgId

String

Required

1760339273

Request ID for one data synchronization. Unique for one synchronization configuration. Requests of duplicate IDs will be ignored. The ID must be no more than 100 bytes.

osType

String

No

iOS/Android

Specifies the operating system of the mobile phone to which the data is to be pushed. By default, no parameters will be passed, that is, no specifications, and data will be pushed to both iOS and Android platforms.

appMinVersion

String

No

0.0.0.0

Specify the client version to which the data is pushed. Data is sent only to clients of the specified or later versions.

appMaxVersion

String

No

100.100.100.100

Specify the client version to which the data is pushed. Data is sent only to clients of the specified or earlier versions.

validTimeStart

String

No

1584448493913

Data will be pushed only when the current time is later than or equal to validTimeStart.

validTimeEnd

String

No

1584452093913

Data will be pushed only when the current time is earlier than or equal to validTimeEnd.

Result codes

Result code

Description

Solution

Success

Synchronization succeeded.

Synchronization succeeded.

ARGS_IS_NULL

Required parameters are empty

Check if the parameters have been completely passed according to the non-empty logical operation.

PAYLOAD_LONG

PAYLOAD message body is too long

Check if the length of the playload property parameter exceeds the limit.

THIRD_MSG_ID_LONG

Third-party service ID is too long.

Check if the third-party service ID exceeds the limit.

BIZ_NOT_ONLINE

The synchronization identifier of the service scenario is not submitted.

Go to mPaaS Console > Mobile Sync Service to check if the bizType synchronization identifier has been configured and submitted.

THIRD_MSG_ID_IS_NULL

Third-party service ID is empty

Check if the third-party service ID is empty.

SYSTEM_ERROR

System error

Contact technical support to confirm the cause of system errors.

INVALID_TENANT_ID

Invalid tenant ID

Check if the App ID is correct and If you have the permission to use the App ID.

Sample code

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.mpaas.model.v20201028.CreateOpenSingleDataRequest;
import com.aliyuncs.mpaas.model.v20201028.CreateOpenSingleDataResponse;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class MsyncPopDemo {

    public static void main(String[] args) {
        //Request information, except AccessKey ID AccessKey Secret can be fixed
        DefaultProfile.addEndpoint("cn-hangzhou", "mpaas", "mpaas.cn-hangzhou.aliyuncs.com");

        // Alibaba Cloud account AccessKey has access rights to all APIs. It is recommended that you use RAM users for API access or daily operation and maintenance.
        // It is strongly recommended not to save the AccessKey ID and AccessKey Secret in the project code, otherwise the AccessKey may be leaked, threatening the security of all resources under your account.
        // This example uses saving the AccessKey ID and AccessKey Secret in environment variables as an example. You can also save it to the configuration file according to business needs.
        String accessKeyId = System.getenv("MPAAS_AK_ENV");
        String accessKeySecret = System.getenv("MPAAS_SK_ENV");

        // Create a DefaultAcsClient instance and initialize it
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou", // Region ID
                accessKeyId,   // AccessKey ID of RAM account
                accessKeySecret);  // AccessKey Secret of RAM account
        IAcsClient client = new DefaultAcsClient(profile);

        CreateOpenSingleDataRequest singleRequest = constructSingleRequest();

        CreateOpenSingleDataResponse singleDataResponse;
        try {
            singleDataResponse = client.getAcsResponse(singleRequest);
            System.out.println("singleDataResponse:" +
                    ToStringBuilder
                            .reflectionToString(singleDataResponse, ToStringStyle.SHORT_PREFIX_STYLE));

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    private static CreateOpenSingleDataRequest constructSingleRequest() {

        CreateOpenSingleDataRequest singleRequest
                = new CreateOpenSingleDataRequest();
        //*************Required properties*************/
 
        //App ID obtained from the mPaaS console
        singleRequest.setAppId("xxxxxxx");
        //WorkspaceId obtained from the mPaaS console
        singleRequest.setWorkspaceId("xxxxxxxx");
        //The synchronization identifier configured during mobile synchronization in the mPaaS console
        singleRequest.setBizType("TEST-SYNC");
        //User ID or device ID to be pushed (UTDID)
        singleRequest.setLinkToken("testUserId");
        //Actual service message body, custom format with not more than 4096 characters in length.
        singleRequest.setPayload("testPayload");
         //Service ID, unique, not more than 100 characters in length.
        singleRequest.setThirdMsgId("test_third_msg_id_" + System.currentTimeMillis());
 
        //************Non-required properties*************/
 
        //No restriction on the operating system when the operating system of the target device, iOS or Android, is empty.
        singleRequest.setOsType("IOS");
        //Minimum client version supported, such as 8.6.0.0.9999. If the version specified here is empty, there will be no limit on the minimum client version.
        singleRequest.setAppMinVersion("0.0.0.0");
        //Maximum client version supported, such as 9.0.0.0.9999. If the version specified here is empty, there will be no limit on the maximum client version.
        singleRequest.setAppMaxVersion("100.100.100.100");
        //Start of the validity period. If it is empty, there will be no limit on the start of the validity period.
        singleRequest.setValidTimeStart(System.currentTimeMillis());
        //End of the validity period. If it is empty, there will be no limit on the end of the validity period. The longest validity period is 30 days.
        singleRequest.setValidTimeEnd(System.currentTimeMillis() + (1000 * 3600));
 
        return singleRequest;
 
    }

Global data synchronization interface

Global data synchronization interface is used to synchronize data to all devices.

Parameters

Business parameters are as follows:

Parameter

Data type

Required

Example

Description

appId

String

Required

ONEX570DA892117

Get App ID from the mPaaS console.

workspaceId

String

Required

PROD

Get Workspace ID from the mPaaS console.

bizType

String

Required

UCHAT

The synchronization identifier configured in the mPaaS console. See Console introduction for more details.

payload

String

Required

testtestatapalayd

Actual service message body, custom format with not more than 4096 characters in length.

thirdMsgId

String

Required

1760339273

One data synchronization request ID. Unique for one synchronization identifier. Requests from duplicate IDs will be ignored. The ID must be no more than 100 bytes.

osType

String

No

IOS/ANDROID

Specifies the operating system of the mobile phone to which the data is to be pushed. By default no parameters will be passed, that is, no specifications, and data will be pushed to both iOS and Android platforms.

appMinVersion

String

No

0.0.0.0

Specify the client version to which the data is pushed. Data is sent only to clients of the specified or later versions.

appMaxVersion

String

No

100.100.100.100

Specify the client version to which the data is pushed. Data is sent only to clients of the specified or earlier versions.

validTimeStart

String

No

1584448493913

Data will be pushed only when the current time is later than or equal to validTimeStart.

validTimeEnd

String

No

1584452093913

Data will be pushed only when the current time is earlier than or equal to validTimeEnd.

maxUid

Long

No

99

The maximum Uid in the synchronization range. Uid is the second last character and the third last character of the user ID or device ID. If the Uid is not alphabetic, you need to convert the Uid to ASCII.

minUid

Long

No

00

The minimum Uid in the synchronization range. Uid is the second last character and the third last character of the user ID or device ID. If the Uid is not alphabetic, you need to convert the Uid to ASCII.

uids

String

No

01,02,99

The priority is higher than maxUid and minUid.

The discrete Uid segment. Uid is the second last character and the third last character of the user ID or device ID. If the Uid is not alphabetic, you need to convert the Uid to ASCII.

Result codes

Result code

Description

Solution

Success

The task is successful.

The task is successful.

ARGS_IS_NULL

Required parameters are empty

Check if the parameters have been completely passed according to the non-empty logical operation.

PAYLOAD_LONG

PAYLOAD message body is too long

Check if the length of the playload property parameter exceeds the limit.

THIRD_MSG_ID_LONG

Third-party service ID is too long.

Check if the third-party service ID exceeds the limit.

BIZ_NOT_ONLINE

The synchronization identifier of the service scenario is not submitted.

Go to mPaaS Console > Mobile Sync Service to check if the bizType synchronization identifier has been configured and submitted.

THIRD_MSG_ID_IS_NULL

Third-party service ID is empty

Check if the third-party service ID is empty.

SYSTEM_ERROR

System error

Contact technical support to confirm the cause of system errors.

NOT_SUPPORT_GLOBAL

Does not support calls with global service synchronization identifier

According to BizType, go to mPaaS Console > Mobile Sync Service to check if the synchronization identifier is user-based or device-based.

INVALID_TENANT_ID

Invalid tenant ID

Check if the App ID is correct and If you have the permission to use the App ID.

Sample code

public static void main(String[] args) {

        //Request information, fixed except AccessKey ID and AccessKey secret
        DefaultProfile.addEndpoint("cn-hongkong", "mpaas", "mpaas.cn-hongkong.aliyuncs.com");
        // Create and initialize a DefaultAcsClient instance.
        DefaultProfile profile = DefaultProfile.getProfile(
            "cn-hongkong", // Region ID
            "xxxxxx", // AccessKey ID of the RAM account 
            "xxxxxx"); // AccessKey secret of the RAM account 
        IAcsClient client = new DefaultAcsClient(profile);

        CreateOpenGlobalDataRequest globalDataRequest = constuctGlobelRequest();

        CreateOpenGlobalDataResponse globalDataResponse;
        try {
            globalDataResponse = client.getAcsResponse(globalDataRequest);
            System.out.println("globalDataResponse:" +
                ToStringBuilder
                    .reflectionToString(globalDataResponse, ToStringStyle.SHORT_PREFIX_STYLE));

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

 private static CreateOpenGlobalDataRequest constuctGlobelRequest() {

        CreateOpenGlobalDataRequest globalRequest
            = new CreateOpenGlobalDataRequest();

        //************Required properties*************/
        //App ID obtained from the mPaaS console
        globalRequest.setAppId("BE9C457161429");
        //WorkspaceId obtained from the mPaaS console
        globalRequest.setWorkspaceId("sit");
        //The synchronization identifier configured during mobile synchronization in the mPaaS console
        globalRequest.setBizType("test-global");
        //Actual service message body, custom format with not more than 4096 characters in length.
        globalRequest.setPayload("testtestata");
        //Service ID, unique, not more than 100 characters in length.
        globalRequest.setThirdMsgId("test_third_msg_id_" + System.currentTimeMillis());

        //************Non-required properties*************/

        //No restriction on the operating system when the operating system of the target device, iOS or Android, is empty.
        globalRequest.setOsType("IOS");
        //Minimum client version supported, such as 8.6.0.0.9999. If the version specified here is empty, there will be no limit on the minimum client version.
        globalRequest.setAppMinVersion("0.0.0.0");
        //Maximum client version supported, such as 9.0.0.0.9999. If the version specified here is empty, there will be no limit on the maximum client version.

        globalRequest.setAppMaxVersion("100.100.100.100");
        //Maximum Uid
        globalRequest.setMaxUid(Long.valueOf(99));
        //Minimum Uid
        globalRequest.setMinUid(Long.valueOf(1));
        //Uid 00-99 to be pushed for the phased-release, which is a string array.
        globalRequest.setUids("01,02,99");

        globalRequest.setValidTimeStart(System.currentTimeMillis());
        globalRequest.setValidTimeEnd(System.currentTimeMillis() + (1000 * 3600));

        return globalRequest;

    }